Using the cvGet2D OpenCV Function

I am trying to get information from an image using the cvGet2D function in OpenCV.

I created an array of 10 IplImage pointers:

 IplImage *imageArray[10]; 

and I save 10 images from a webcam:

 imageArray[numPicture] = cvQueryFrame(capture); 

when i call the function:

 info = cvGet2D(imageArray[0], 250, 100); 

where info :

 CvScalar info; 

I got an error:

OpenCV error: invalid argument (unrecognized or unsupported array type) in cvPtr2D, file / build / buildd / opencv -2.1.0 / src / cxcore / cxarray.cpp, line 1824
terminate call after calling instance 'cv :: Exception'
what (): / build / buildd / opencv-2.1.0 / src / cxcore / cxarray.cpp: 1824: error: (-5) unrecognized or unsupported array type in cvPtr2D function

If I use the cvLoadImage function to initialize the IplImage pointer and then pass it to the cvGet2D function, the code works correctly:

 IplImage* imagen = cvLoadImage("test0.jpg"); info = cvGet2D(imagen, 250, 100); 

however, I want to use information already stored in my array.

Do you know how I can solve it?

+4
source share
3 answers

Yes, the message is correct.

If you want to keep the pixel value, you need to do something like this.

 int value = 0; value = ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels +0]; cout << "pixel value for Blue Channel and (i,j) coordinates: " << value << endl; 

To summarize, in order to build or save data, you must create an integer value (pixel value varies from 0 to 255). But if you want to test the pixel value (for example, in closing an if or something similar), you can access the pixel value directly without using an integer value.

I think this is a little strange when you start, but when you work with it 2 o 3 times, you will work without difficulty.

+9
source

Although this is a very late answer, but I think someone can still find a solution with CvGet2D . Here it is.

For CvGet2D , you must first pass the arguments in Y order, and then X

Example:

 CvScalar s = cvGet2D(img, Y, X); 

It is not mentioned anywhere in the documentation, but you only find it inside core.h / core_c.h . Try moving on to declaring CvGet2D() , and over the function prototypes there are a few comments that explain this.

+12
source

Sorry, cvGet2D is not the best way to get the pixel value. I know its shortest and most understandable way, because you get the pixel value in only one line of code and knowledge of the coordinates.

I offer you this option. When you see this code, you think it is so difficult, but more efficient.

 int main() { // Acquire the image (I'm reading it from a file); IplImage* img = cvLoadImage("image.bmp",1); int i,j,k; // Variables to store image properties int height,width,step,channels; uchar *data; // Variables to store the number of white pixels and a flag int WhiteCount,bWhite; // Acquire image unfo height = img->height; width = img->width; step = img->widthStep; channels = img->nChannels; data = (uchar *)img->imageData; // Begin WhiteCount = 0; for(i=0;i<height;i++) { for(j=0;j<width;j++) { // Go through each channel of the image (R,G, and B) to see if it equal to 255 bWhite = 0; for(k=0;k<channels;k++) { // This checks if the pixel kth channel is 255 - it can be faster. if (data[i*step+j*channels+k]==255) bWhite = 1; else { bWhite = 0; break; } } if(bWhite == 1) WhiteCount++; } } printf("Percentage: %f%%",100.0*WhiteCount/(height*width)); return 0; 

This code highlights white pixels and gives you the percentage of white image in the image.

+1
source

Source: https://habr.com/ru/post/1341399/


All Articles