본문 바로가기

OpenCV

ch06 logical

#python code

import cv2

def main():
    src1 = cv2.imread("D:\\projects\\SampleCode\\006939-master\\ch06\\arithmetic\\lenna256.bmp", cv2.IMREAD_GRAYSCALE)
    src2 = cv2.imread("D:\\projects\\SampleCode\\006939-master\\ch06\\arithmetic\\square.bmp", cv2.IMREAD_GRAYSCALE)

    if src1 is None or src2 is None:
        print("Image load failed!")
        return

    cv2.imshow("src1", src1)
    cv2.imshow("src2", src2)

    dst1 = cv2.bitwise_and(src1, src2)
    dst2 = cv2.bitwise_or(src1, src2)
    dst3 = cv2.bitwise_xor(src1, src2)
    dst4 = cv2.bitwise_not(src1)

    cv2.imshow("dst1", dst1)
    cv2.imshow("dst2", dst2)
    cv2.imshow("dst3", dst3)
    cv2.imshow("dst4", dst4)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

// C++ code

#include "opencv2/opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(void)
{
	Mat src1 = imread("lenna256.bmp", IMREAD_GRAYSCALE);
	Mat src2 = imread("square.bmp", IMREAD_GRAYSCALE);

	if (src1.empty() || src2.empty()) {
		cerr << "Image load failed!" << endl;
		return -1;
	}

	imshow("src1", src1);
	imshow("src2", src2);

	Mat dst1, dst2, dst3, dst4;

	bitwise_and(src1, src2, dst1);
	bitwise_or(src1, src2, dst2);
	bitwise_xor(src1, src2, dst3);
	bitwise_not(src1, dst4);

	imshow("dst1", dst1);
	imshow("dst2", dst2);
	imshow("dst3", dst3);
	imshow("dst4", dst4);
	waitKey();

	return 0;
}

C++ Code 출처 : OpenCV 4로 배우는 컴퓨터 비전과 머신 러닝 - 황선규 저


 

'OpenCV' 카테고리의 다른 글

ch07 filter  (0) 2024.05.20
ch07 blurring  (0) 2024.05.20
ch06 arithmetic  (0) 2024.05.20
ch05 contrast  (0) 2024.05.19
ch05 brightness  (0) 2024.05.19