# Understanding bitwise_and operation

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.

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?

----.

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]


[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 close merge delete

Sort by » oldest newest most voted

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);

more

"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.

( 2016-09-20 20:21:41 -0500 )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...

( 2016-09-20 20:23:27 -0500 )edit

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

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

( 2016-09-20 22:56:30 -0500 )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?

( 2016-09-21 00:07:20 -0500 )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.

( 2016-09-21 07:42:11 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Seen: 5,053 times

Last updated: Sep 20 '16