python
import cv2 as cv
import numpy as np
def function():
img1 = np.zeros(shape = [400, 400, 3], dtype = np.uint8)
img2 = np.zeros(shape = [400, 400, 3], dtype = np.uint8)
img1[100: 200, 100: 200, 1] = 255
img1[100: 200, 100: 200, 2] = 255
cv.imshow("img1", img1)
img2[150: 250, 150: 250, 2] = 255
cv.imshow("img2", img2)
dst1 = cv.bitwise_and(img1, img2)
dst2 = cv.bitwise_or(img1, img2)
dst3 = cv.bitwise_xor(img1, img2)
cv.imshow("and", dst1)
cv.imshow("or", dst2)
cv.imshow("xor", dst3)
test = cv.imread("../images/test.jpg")
dst4 = cv.bitwise_not(test)
cv.imshow("input", test)
cv.imshow("not", dst4)
function()
cv.waitKey(0)
cv.destroyAllWindows()
python中的新知识点
主要和c++中对比看吧
c++
#include "all.h"
using namespace std;
using namespace cv;
void MyClass::day007() {
Mat test = read(PATH + "\\images\\test.jpg");
Mat img = Mat::zeros(Size(400, 400), CV_8UC3);
Rect rec(100, 100, 100, 100);
img(rec) = Scalar(0, 0, 255);
imshow("img1", img);
Mat img1 = Mat::zeros(Size(400, 400), CV_8UC3);
rec.x = 150;
rec.y = 150;
img1(rec) = Scalar(0, 0, 255);
imshow("img2", img1);
Mat dst, dst1, dst2, dst3;
bitwise_and(img, img1, dst1);
bitwise_or(img, img1, dst2);
bitwise_xor(img, img1, dst3);
imshow("and", dst1);
imshow("or", dst2);
imshow("xor", dst3);
imshow("input", test);
bitwise_not(test, dst);
imshow("not", dst);
waitKey(0);
}
c++新知识点:
- cv::bitwise_and();
- cv::bitwise_or();
- cv::bitwise_xor();
- cv::bitwise_not();
- cv::Mat的数据类型CV_8UC3;表示3位uchar类型数,共有3个通道
- cv::Size2i::Size();和Mat::size()的返回值是一样的
- cv::Rect
- cv::Mat::Mat(Rect);为图片中Rect范围内的像素赋值
- cv::Scalar::Scalar();为图像中的点赋值
网友评论