Ask Your Question
0

Understanding bitwise_and operation

asked 2016-09-19 21:21:58 -0600

alienmon gravatar image

updated 2016-09-20 09:52:17 -0600

Hi all, I'm new to openCV. I'm a bit confused about bitwise operation. Appreciate if you can help me to clarify some things.

  1. Bitwise operation is for binary image only? cause from the (admittedly limited) code that I've seen, it doesn't seem so. I don't get how AND OR ,etc operation to be applied to non binary ? please explain on this
  2. I read this documentation on bitwise_and.

    dst(I)=src1(I)∧src2(I)if mask(I)≠0

What I understand is that : the result is equal to src1 & src2 , if the mask for that element is not zero. My question is that if mask for that element is zero, what is result equal to? is it equal to the element in src1 or src2?

----.

Lastly, please help me to understand the following code from ROS

image = self.bridge.imgmsg_to_cv2(msg) [0]
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) [1]
lower_yellow = numpy.array([ 50,  50, 170]) [2]
upper_yellow = numpy.array([255, 255, 190]) [3]
mask = cv2.inRange(hsv, lower_yellow, upper_yellow) [4]
masked = cv2.bitwise_and(image, image, mask=mask) [5]

[0] This line is just to convertimage from ROS image format to openCV image format. Shortly image is a RGB image.

[1] convert image form RGB to HSV

[2] set the lower limit for yellow color

[3] set the upper limit for yellow color

[4] producing a binary image : the element that is in the range of lower_yellow and upper_yellow gives 1, and the rest give 0. So mask is a binary image, with the section that qualify as yelloq =1 , and 0 for the rest.

[5] I don't really understand this part. We do bitwise_and operation on image & with itself? What for? Also, image is RGB ,not binary, how to operate AND on RGB? Please explain

Thanks

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2016-09-20 11:44:00 -0600

Tetragramm gravatar image

They are doing something strange. Normally you do use one of the inputs as binary. For an RGB image, your binary image would be 3 channels, with 255 being ON, and 0 being OFF. The on remain the same, and the off become black. There might be a few occasions where two non-binary images are used. After all, the images are stored in binary and they can be manipulated that way. Perhaps make all the values even by doing a binary_and on a value with only the last bit unset.

They are using this in a way that may not be entirely safe. Where the mask is empty, the result in the output is unchanged from what is there before. So if masked is filled with garbage instead of zeros...

In short, they are doing what in c++ would be this. However, I don't know if Python has the setTo method, it's not in the tutorials. Also, they might be saving some time, but I don't think so.

bitwise_not(mask, mask);
image.setTo(0, mask);
edit flag offensive delete link more

Comments

Hi, thanks for the answer. But I don't rlly understand your answer.

"They are doing something strange" -> which one? "do use one of the inputs as binary"-> you mean normally one of the input in bitwise_and operation is binary, what about the other input?

" For an RGB image, your binary image would be 3 channels, with 255 being ON, and 0 being OFF" -> what about the number between 0 and 255 ?

"Perhaps make all the values even by doing a binary_and on a value with only the last bit unset" -> i don't understand this

"Where the mask is empty, the result in the output is unchanged from what is THERE before" -> there is referring to what? source1, source2 , or what? cause masked is a new variable that we're going to fill with the result of the operation.

alienmon gravatar imagealienmon ( 2016-09-20 20:21:41 -0600 )edit

Lastly I don't understand the piece of code you wrote. "bitwise_not(mask, mask);"-> what is it for? and it's not saved to any variable or used later as well...

Please help to clarify. Thank you

alienmon gravatar imagealienmon ( 2016-09-20 20:23:27 -0600 )edit

Do you know how any bitwise operation works? Like, with a single int?

https://en.wikipedia.org/wiki/Bitwise...

Tetragramm gravatar imageTetragramm ( 2016-09-20 22:56:30 -0600 )edit

So let's say for bitwise operation between 2 RGB images. We do the operation on 3 channels, red with red, g with g ,b with b.

For every channel, e.g. red, let's say we have 130 and 120 , then we convert 130 ,120 into binary, and do bitwise_and operation on the two binary numbers resulting to a binary number. We convert back into decimal, resulting to the R value of the final image. Is that right?

alienmon gravatar imagealienmon ( 2016-09-21 00:07:20 -0600 )edit

No. It is already binary. It is always binary. Nothing in your computer is anything but binary. Anything that appear not-binary is an illusion created by clever combinations of binariness.

130 is, as stored in memory 0b10000010. So when you do a bitwise operation, it simply starts at the beginning of each image, and goes to the end, performing the bitwise operation on every bit. Usually, more than one at once, since what happens to one bit doesn't change what happens to the others.

130 is 0b10000010 and 120 is 0b01111000. If you do a bitwise_and on these, you may notice not a single bit overlaps. So the result is 0. If you do a bitwise_or, you get 250. That's because with no overlap, it's the same as addition. 130 OR 131 is 131, because only the 1 bit is different.

Tetragramm gravatar imageTetragramm ( 2016-09-21 07:42:11 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-09-19 21:21:58 -0600

Seen: 6,430 times

Last updated: Sep 20 '16