OpenCV - Intersection between two binary images

≯℡__Kan透↙ 提交于 2019-12-20 09:37:39

问题


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


回答1:


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:

Here is the result of applying cv::bitwise_and:

Here is the result of applying cv::bitwise_or:

Here is the result of applying cv::bitwise_xor:




回答2:


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



来源:https://stackoverflow.com/questions/11262312/opencv-intersection-between-two-binary-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!