Ask Your Question
0

extract a vector of pixels from a frame

asked Dec 19 '12

4arbouch gravatar image

Hello,

I'am actually working in a project on android in which I want to extract some specific pixel values (according to a condition) and having these values in a vector. I will use this vector of the chosen pixels to do some operation ( the mean value for exemple ) , Is there a method in OpenCV that can help me doing this ?

Thank you :)

Preview: (hide)

2 answers

Sort by » oldest newest most voted
0

answered Dec 20 '12

Haris gravatar image

updated Dec 21 '12

Using below code you can easily access image pixel from a Mat, from that R,G,B value you can check your condition and store the pixel value.

 for(int i = 0; i < img.rows; i++)
   {
  for(int j = 0; j < img.cols; j++)
   { 
    Vec3b bgr = img.at<Vec3b>(i, j);

    // using BGR values check the condition and store the pixel value
   }
  }
Preview: (hide)

Comments

Hi , Thank you for your response :) But if we use loops, this will considerably increases the complexity of the program and the application will be very slow in processing especially when doing such image processing in real time, what I'am actually doing :) So I asked myself if there is a method in openCV that I can use to do such operations as described in the comment below . Thank you :)

4arbouch gravatar image4arbouch (Dec 27 '12)edit
0

answered Dec 20 '12

You can use mean function in C++ and Core.mean() from Java. It is native OpenCV function and it is rather faster then individual pixel access from Java code.

Preview: (hide)

Comments

Hi , My goal is not to calculate the mean of all the image, I want to calculate the mean of some regions of the images. In fact , I want to calculate the mean value of the non zeros pixels , When I use Core.mean the 0 values of the pixels will contribute in this calculation ! So I asked my self if there is a method in openCV that can help me doing this. : Calculate the mean value of just some pixels on the image ( for me the values that are different of 0 ) . Thank you :)

4arbouch gravatar image4arbouch (Dec 27 '12)edit

Mean function supports mask. It applies only for pixels, that has non-zero value in mask. You can use your input mat as mask too (if input mat is grey scale). So, you get mean for all non-zero elements. Also you can create separate one channel mat with mask using threshold function or other OpenCV functions, that operates with single channel matrices, i.e. dilate, erode, etc.

Thank you :) it works :D

4arbouch gravatar image4arbouch (Dec 29 '12)edit

Question Tools

Stats

Asked: Dec 19 '12

Seen: 1,422 times

Last updated: Dec 20 '12