Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV - Intersection between two binary images

Tags:

Let's say I have two binary images of the same size. How do I find the intersection between the two binary images? Only pixels of the same coordinate (location) on the two images that are white (gray - 255) will give white pixels on the output image (intersection).

like image 555
XterNalz Avatar asked Jun 29 '12 13:06

XterNalz


People also ask

How to perform bitwise AND operation on two images using Java OpenCV?

How to perform Bitwise And operation on two images using Java OpenCV? You can compute bitwise conjunction between two images using the bitwise_and () method of the org.opencv.core.Core class.

How do I add two images in OpenCV?

You can add two images with the OpenCV function, cv.add (), or simply by the numpy operation res = img1 + img2. Both images should be of same depth and type, or the second image can just be a scalar value.

What is the difference between NumPy and OpenCV addition?

Both images should be of same depth and type, or the second image can just be a scalar value. There is a difference between OpenCV addition and Numpy addition. OpenCV addition is a saturated operation while Numpy addition is a modulo operation.

What are bitwise operations used in image manipulation?

Bitwise operations are used in image manipulation and used for extracting essential parts in the image. In this article, Bitwise operations used are : Also, Bitwise operations helps in image masking. Image creation can be enabled with the help of these operations. These operations can be helpful in enhancing the properties of the input images.


2 Answers

You can use cvAnd or cv::bitwise_and on the two images. The resulting image will be white only where both the input images are white.

EDIT: Here are the results of applying cv::bitwise_and, cv::bitwise_or and cv::bitwise_xor on binary images:

These are the two source images:

image 1image 2

Here is the result of applying cv::bitwise_and:

imgAnd

Here is the result of applying cv::bitwise_or:

imgOr

Here is the result of applying cv::bitwise_xor:

imgXor

like image 127
Ove Avatar answered Sep 24 '22 14:09

Ove


Here's how to do this in python (with the images above):

import cv2  img1 = cv2.imread('black_top_right_triangle.png',0) img2 = cv2.imread('black_bottom_right_triangle.png',0)  img_bwa = cv2.bitwise_and(img1,img2) img_bwo = cv2.bitwise_or(img1,img2) img_bwx = cv2.bitwise_xor(img1,img2)  cv2.imshow("Bitwise AND of Image 1 and 2", img_bwa) cv2.imshow("Bitwise OR of Image 1 and 2", img_bwo) cv2.imshow("Bitwise XOR of Image 1 and 2", img_bwx) cv2.waitKey(0) cv2.destroyAllWindows() 

If you need to install OpenCV for Python, save time by following these directions (installation has historically been quite a pain).

like image 32
zelusp Avatar answered Sep 22 '22 14:09

zelusp