开发者

Why does cvSet2D take in a tuple of doubles, and why is this tuple all 0 save for the first element?

开发者 https://www.devze.com 2023-04-08 10:07 出处:网络
cvSet2D(matrix, i, j, tuple) Hi. I\'m dissecting the Gabor Filter code given in http://www.eml.ele.cst.nihon-u.ac.jp/~momma/wiki/wiki.cgi/OpenCV/Gabor%20Filter.html . I have a few questions on cvSet2

cvSet2D(matrix, i, j, tuple)

Hi. I'm dissecting the Gabor Filter code given in http://www.eml.ele.cst.nihon-u.ac.jp/~momma/wiki/wiki.cgi/OpenCV/Gabor%20Filter.html . I have a few questions on cvSet2D especially as used in the following code snippets (as given in the link):

C code:

for (x = -kernel_size/2;x<=kernel_size/2; x++) {
  for (y = -kernel_size/2;y<=kernel_size/2; y++) {
    kernel_val = exp( -((x*x)+(y*y))/(2*var))*cos( w*x*cos(phase)+w*y*sin(phase)+psi);
    cvSet2D(kernel,y+kernel_size/2,x+kernel_size/2,cvScalar(kernel_val));
    cvSet2D(kernelimg,y+kernel_size/2,x+kernel_size/2,cvScalar(kernel_val/2+0.5));
  }
}

Python code:

for x in range(-kernel_size/2+1,kernel_size/2+1):
      for y in range(-kernel_size/2+1,kernel_size开发者_开发知识库/2+1):
          kernel_val = math.exp( -((x*x)+(y*y))/(2*var))*math.cos( w*x*math.cos(phase)+w*y*math.sin(phase)+psi)
          cvSet2D(kernel,y+kernel_size/2,x+kernel_size/2,cvScalar(kernel_val))
          cvSet2D(kernelimg,y+kernel_size/2,x+kernel_size/2,cvScalar(kernel_val/2+0.5))

As I'm aware cvSet2D sets the (j, i)th pixel of a matrix to the equivalent color value of the tuple defined in the last parameter. But why does it take in a tuple of doubles? Isn't it more natural to take in a tuple of ints, seeing that a pixel color is defines as a tuple of ints?

Lastly, if I read the docs correctly, the cvScalar method used returns the 4-tuple <given_value_in_double, 0.000000, 0.000000, 0.000000)>. I surmised that cvSet2D takes the first three values and uses it to as the RGB 3-tuple. But, seeing that the output of Gabor Filters are more or less in grayscale, that won't hold being that, the colors produced in my scheme will lean towards red. So, what does cvSet2D do with this tuple?

Thank you for anyone who'll take the bother to explain!


In OpenCV, images can have 1 (grayscale), 2, 3 (RGB) or 4 (RGB plus alpha) channels. The one Set2D function is used for all images regardless of how many channels they have. Each of the elements of that tuple is used for specifying a channel value.

You always pass in four values through the tuple, but OpenCV will only use the first N of these, where N is the number of channels in the image. It will ignore the rest. Zero is just an accepted place-holder that means "this value does not matter". Of course, you can pass in any value you want, but the readability of your code will suffer.

The reason double is used as the argument type is probably because that is the highest precision type in Python that you can use. OpenCV will cast this value to the proper underlying type (which it gets from the image).

Since you're dealing with one-channel 32-bit float images (CV_32FC1), just continue using Set2D(image, (value, 0, 0, 0)) and everything should be alright.


I think that for grayscale images (matrices), CvSet2D sets the only channel (brightness) using the first value in the cvScalar. The documentation for CvSet2D seems broken, I'll try to verify in the code.

EDIT: In the code sample you linked to, kernel has type CV_32FC1 which means it has one channel only. So does kernelImg:

kernel = cvCreateMat(kernel_size,kernel_size,CV_32FC1)
kernelimg = cvCreateImage(cvSize(kernel_size,kernel_size),IPL_DEPTH_32F,1)

So it makes sense that the scalar only needs one channel value set within.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号