linux python 笔记
纯粹个人笔记好查找
- 创建Python软链接
sudo ln -s /usr/bin/python3.8 /usr/bin/python
Python对应的OpenCV可以很好看到图片矩阵中的内容(c++我只会看到指针地址),用于了解rgba数据存储格式,便于以后图片分析
import cv2 as cv
import numpy as np
fn = 'fruits.jpg'
img = cv.imread(cv.samples.findFile(fn))
rgba=cv.cvtColor(img,cv.COLOR_RGB2RGBA)
cv.imshow("fruits",rgba)
cv.waitKey()
cv.destroyAllWindows()
以下0-3分别对应图片中的rgba四个通道;a透明度全为0xFF
图片按像素处理python
def assignment_new_image(image):
(height,width,channels)=image.shape #获取图高、宽、通道
dest_image = np.zeros([height,width,channels],np.uint8)
for row in range(height):
for col in range(width):
for c in range(channels):
pv=image[row,col,c]
dest_image[row,col,c]=pv
return dest_image
图片按像素操作(C++)
Mat src = imread("test/2715DTZ.jpg");
imshow("src", src);
Mat rgba;
cvtColor(src, rgba, COLOR_BGR2BGRA);
imshow("rgba", rgba);
// Mat new_img;
// new_img.create(rgba.size(), rgba.type());//
Mat new_img(rgba.rows, rgba.cols, CV_8UC4);
int channel = rgba.channels(); //
for (int i = 0; i < rgba.rows; i++)
{
for (int j = 0; j < rgba.cols; j++)
{
if (channel == 1)
{
new_img.at<uchar>(i, j) = rgba.at<uchar>(i, j);
}
else if (channel == 4)
{
// cout << "i=" << i << ";j=" << j << endl;
new_img.at<Vec4b>(i, j)[0] = rgba.at<Vec4b>(i, j)[0];
new_img.at<Vec4b>(i, j)[1] = rgba.at<Vec4b>(i, j)[1];
new_img.at<Vec4b>(i, j)[2] = rgba.at<Vec4b>(i, j)[2];
new_img.at<Vec4b>(i, j)[3] = rgba.at<Vec4b>(i, j)[3];
}
}
}
imshow("new_img", new_img);
ndk中对YUV格式图片转换成RGBA格式图片
/*
* PresentImage90()
* Converting YUV to RGB
* Rotation image anti-clockwise 90 degree -- (x, y) --> (-y, x)
*/
void ImageReader::PresentImage90(ANativeWindow_Buffer *buf, AImage *image) {
AImageCropRect srcRect;
AImage_getCropRect(image, &srcRect);
int32_t yStride, uvStride;
uint8_t *yPixel, *uPixel, *vPixel;
int32_t yLen, uLen, vLen;
AImage_getPlaneRowStride(image, 0, &yStride);
AImage_getPlaneRowStride(image, 1, &uvStride);
AImage_getPlaneData(image, 0, &yPixel, &yLen);
AImage_getPlaneData(image, 1, &vPixel, &vLen);
AImage_getPlaneData(image, 2, &uPixel, &uLen);
int32_t uvPixelStride;
AImage_getPlanePixelStride(image, 1, &uvPixelStride);
// Mat src = imread("/sdcard/DCIM/Camera/capture.jpg");
int32_t height = MIN(buf->width, (srcRect.bottom - srcRect.top));
int32_t width = MIN(buf->height, (srcRect.right - srcRect.left));
Mat dst_src(height, width, CV_8UC4);
uint32_t *out = static_cast<uint32_t *>(buf->bits);
out += height - 1;
for (int32_t y = 0; y < height; y++) {
const uint8_t *pY = yPixel + yStride * (y + srcRect.top) + srcRect.left;
int32_t uv_row_start = uvStride * ((y + srcRect.top) >> 1);
const uint8_t *pU = uPixel + uv_row_start + (srcRect.left >> 1);
const uint8_t *pV = vPixel + uv_row_start + (srcRect.left >> 1);
for (int32_t x = 0; x < width; x++) {
const int32_t uv_offset = (x >> 1) * uvPixelStride;
// [x, y]--> [-y, x]
out[x * buf->stride] = YUV2RGB(pY[x], pU[uv_offset], pV[uv_offset]);
int32_t pix = YUV2RGB(pY[x], pU[uv_offset], pV[uv_offset]);
uchar r=((pix&0x00ff0000)>>16);
uchar g=((pix&0x0000ff00)>>8);
uchar b=((pix&0x000000ff));
dst_src.at<Vec4b>(y, x)[0] = r;
dst_src.at<Vec4b>(y, x)[1] = g;
dst_src.at<Vec4b>(y, x)[2] = b;
dst_src.at<Vec4b>(y, x)[3] = 255;
}
out -= 1; // move to the next column
}
imwrite("/sdcard/DCIM/Camera/capture1.jpg", dst_src);
}
图片多通道合并并展示
clock_t startTime, endTime;
uchar arr[4][height][width];
uchar arr1[4 * height * width];
int channel = rgba.channels(); //
startTime = clock(); //计时开始
for (int i = 0; i < rgba.rows; i++)
{
for (int j = 0; j < rgba.cols; j++)
{
if (channel == 1)
{
new_img.at<uchar>(i, j) = rgba.at<uchar>(i, j);
}
else if (channel == 4)
{
// cout << "i=" << i << ";j=" << j << endl;
new_img.at<Vec4b>(i, j)[0] = rgba.at<Vec4b>(i, j)[0];
new_img.at<Vec4b>(i, j)[1] = rgba.at<Vec4b>(i, j)[1];
new_img.at<Vec4b>(i, j)[2] = rgba.at<Vec4b>(i, j)[2];
new_img.at<Vec4b>(i, j)[3] = rgba.at<Vec4b>(i, j)[3];
// uchar b = rgba.at<Vec4b>(i, j)[0];
// uchar g = rgba.at<Vec4b>(i, j)[1];
// uchar r = rgba.at<Vec4b>(i, j)[2];
// uchar a = rgba.at<Vec4b>(i, j)[3];
// int y = i;
// int x = j;
// arr[0][y][x] = b;
// arr[1][y][x] = g;
// arr[2][y][x] = r;
// arr[3][y][x] = a;
// *(arr1 + i * width + j) = b;
// *(arr1 + height * width + i * width + j) = g;
// *(arr1 + height * width * 2 + i * width + j) = r;
// *(arr1 + height * width * 3 + i * width + j) = a;
}
}
}
endTime = clock(); //计时结束
cout << "The run time is: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
vector<Mat> channels1;
// Mat arr_img(Size(width, height), CV_8UC4, arr);
Mat arr_img;
// split(arr_img, channels1);
channels1.push_back(Mat(Size(width, height), CV_8UC1, arr[0]));
channels1.push_back(Mat(Size(width, height), CV_8UC1, arr[1]));
channels1.push_back(Mat(Size(width, height), CV_8UC1, arr[2]));
channels1.push_back(Mat(Size(width, height), CV_8UC1, arr[3]));
Mat arr_img1(Size(width, height), CV_8UC4, arr1);
cvtColor(arr_img1, arr_img1, COLOR_RGBA2RGB);
merge(channels1, arr_img);
imshow("arr_img", arr_img);