我有以下访问 IplImage 中像素的 C++ 代码。这里它有一个属性叫做 IplImage 中的图像数据。我想将以下代码转换为 OpenCV Android。
IplImage *imageIn = cvLoadImage(strFileImage, CV_LOAD_IMAGE_UNCHANGED);
IplImage *imageShirtHSV = cvCreateImage(cvGetSize(imageShirt), 8, 3);
cvCvtColor(imageShirt, imageShirtHSV, CV_BGR2HSV);
int h = imageShirtHSV->height;
int w = imageShirtHSV->width;
int rowSize = imageShirtHSV->widthStep;
char *imOfs = imageShirtHSV->imageData;
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
// Get the HSV pixel components
uchar H = *(uchar*)(imOfs + y*rowSize + x*3 + 0); // Hue
uchar S = *(uchar*)(imOfs + y*rowSize + x*3 + 1); // Saturation
uchar V = *(uchar*)(imOfs + y*rowSize + x*3 + 2); // Value (Brightness)
}
}
这就是我将上述 c++ 代码转换为 OpenCV Android 的方式。我使用 double[] pixels = imageShirtHSV.get(y, x);
作为 C++ 中的 char *imOfs = imageShirtHSV->imageData;
的等价物。但是当我访问 double[] 像素数组中的 HSV 值时,以下代码给出了 ArrayIndexOutOfBoundsException。
imageIn = inputFrame.rgba();
Imgproc.cvtColor(imageIn, imageShirtHSV, Imgproc.COLOR_BGR2HSV);
int h = imageShirtHSV.height();
int w = imageShirtHSV.width();
int rowSize = (int)imageShirtHSV.step1();
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
double[] pixels = imageShirtHSV.get(y, x);
int hVal = (int)pixels[(y*rowSize) + (x) + 0]; // Hue
int sVal = (int)pixels[(y*rowSize) + (x) + 1]; // Saturation
int vVal = (int)pixels[(y*rowSize) + (x) + 2]; // Value (Brightness)
}
}
有人可以告诉我如何将 c++ char *imOfs = imageShirtHSV->imageData;
行转换为 OpenCV Android 代码。非常感谢。
最佳答案
这个:
double[] pixels = imageShirtHSV.get(y, x);
只给你一个单个像素。你不能像那样处理邻居值。
相反,分配一个数组来保存它们的所有,并访问它: (这也快多了!)
int h = imageShirtHSV.height();
int rowSize = (int)imageShirtHSV.step1();
byte[] pixels = new byte[h*rowSize];
imageShirtHSV.get(0,0,pixels);
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
int hVal = (int)pixels[(y*rowSize) + (x) + 0]; // Hue
int sVal = (int)pixels[(y*rowSize) + (x) + 1]; // Saturation
int vVal = (int)pixels[(y*rowSize) + (x) + 2]; // Value (Brightness)
}
}
关于android - 在 OpenCV Android 中访问相机帧像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21898533/