├── OpenCV_learn ├── Mat.png ├── README.md ├── code_001 │ ├── 001.cpp │ ├── opencv_001.py │ └── test.png ├── code_002 │ ├── gray.png │ ├── opencv_002.cpp │ ├── opencv_002.py │ └── test.png ├── code_003 │ ├── opencv_003.cpp │ ├── opencv_003.py │ └── test.png ├── code_004 │ ├── code_004.cpp │ ├── opencv_004.py │ └── test.png ├── code_005 │ ├── code_005.cpp │ ├── opencv_005.py │ ├── test0.jpg │ └── test1.jpg ├── code_006 │ ├── code_006.cpp │ ├── opencv_006.py │ ├── test0.jpg │ └── test1.png ├── code_007 │ ├── Mat.png │ ├── opencv_007.cpp │ ├── opencv_007.py │ └── test.png ├── code_008 │ ├── opencv_008.cpp │ ├── opencv_008.py │ └── test.png ├── code_009 │ ├── opencv_009.cpp │ ├── opencv_009.py │ ├── test.jpg │ └── test.png ├── code_010 │ ├── code_010.cpp │ ├── opencv_010.py │ └── test.png ├── code_011 │ ├── opencv_011.cpp │ ├── opencv_011.py │ └── test.png ├── code_012 │ ├── opencv_012.cpp │ └── opencv_012.py ├── code_013 │ ├── opencv_013.cpp │ ├── opencv_013.py │ └── test.png ├── code_014 │ ├── opencv_014.cpp │ ├── opencv_014.py │ └── test.png ├── code_015 │ ├── opencv_015.cpp │ └── opencv_015.py ├── code_016 │ ├── opencv_016.cpp │ ├── opencv_016.py │ └── test.png ├── code_017 │ ├── opencv_017.cpp │ ├── opencv_017.py │ └── test.png ├── code_018 │ ├── opencv_018.cpp │ ├── opencv_018.py │ └── test.png ├── code_019 │ ├── Mat.png │ ├── opencv_019.cpp │ ├── opencv_019.py │ └── test.png ├── code_020 │ ├── Mat.png │ ├── opencv_020.cpp │ ├── opencv_020.py │ └── test.png ├── code_021 │ ├── opencv_021.cpp │ ├── opencv_021.py │ └── test.png ├── code_022 │ ├── opencv_022.cpp │ ├── opencv_022.py │ └── test.png ├── code_023 │ ├── opencv_23.cpp │ └── opencv_23.py ├── code_024 │ ├── opencv_024.cpp │ ├── opencv_024.py │ ├── result.png │ └── test.png ├── code_025 │ ├── opencv_025.cpp │ ├── opencv_025.py │ └── test.png ├── code_026 │ ├── opencv_026.py │ ├── openv_026.cpp │ └── test.png ├── code_027 │ ├── opencv_027.cpp │ ├── opencv_027.py │ └── test.png ├── code_028 │ ├── opencv_028.cpp │ ├── opencv_028.py │ └── test.png ├── code_029 │ ├── opencv_029.cpp │ ├── opencv_029.py │ └── test.png ├── code_030 │ ├── opencv_030.cpp │ ├── opencv_030.py │ └── test.png ├── code_031 │ ├── opencv_031.cpp │ ├── opencv_031.py │ ├── result.png │ └── test.png ├── code_032 │ ├── opencv_032.cpp │ ├── opencv_032.py │ ├── prewitt.png │ ├── robert.png │ └── test.png ├── code_033 │ ├── aplacian.png │ ├── opencv_033.cpp │ ├── opencv_033.py │ └── test.png ├── code_034 │ ├── opencv_034.cpp │ ├── opencv_034.py │ ├── result.png │ └── test.png ├── code_035 │ ├── opencv_035.cpp │ ├── opencv_035.py │ ├── result.png │ └── test.png ├── code_036 │ ├── edge.png │ ├── opencv_036.cpp │ ├── opencv_036.py │ ├── result.png │ └── test.png ├── code_037 │ ├── opencv_037.cpp │ ├── opencv_037.py │ └── test.png ├── code_038 │ ├── opencv_038.cpp │ ├── opencv_038.py │ └── test.png ├── code_039 │ ├── opencv_039.cpp │ ├── opencv_039.py │ ├── test.png │ └── test01.png ├── code_040 │ ├── opencv_040.cpp │ ├── opencv_040.py │ └── test.png ├── code_041 │ ├── opencv_041.cpp │ ├── opencv_041.py │ └── test.png ├── code_042 │ ├── binary_result.png │ ├── opencv_042.cpp │ ├── opencv_042.py │ └── test.png ├── code_043 │ ├── binary_result.png │ ├── opencv_043.cpp │ ├── opencv_043.py │ └── test.png ├── code_044 │ ├── binary_result.png │ ├── opencv_044.cpp │ ├── opencv_044.py │ └── test.png ├── code_045 │ ├── binary_result.png │ ├── opencv_045.cpp │ ├── opencv_045.py │ └── test.png ├── code_046 │ ├── labels.png │ ├── opencv_046.cpp │ ├── opencv_046.py │ └── test.png ├── code_047 │ ├── labels.png │ ├── opencv_047.cpp │ ├── opencv_047.py │ └── test.png ├── code_048 │ ├── opencv_048.cpp │ ├── opencv_048.py │ └── test.png ├── code_049 │ ├── canny_output.png │ ├── contours_analysis.png │ ├── lp.jpg │ ├── lp.png │ ├── lplp.jpg │ ├── opencv_049.cpp │ ├── opencv_049.py │ └── test.png ├── code_050 │ ├── canny_output.png │ ├── contours_analysis.png │ ├── lp.png │ ├── opencv_050.cpp │ ├── opencv_050.py │ └── test.png ├── code_051 │ ├── contours_analysis.png │ ├── opencv_051.cpp │ ├── opencv_051.py │ └── test.png ├── code_052 │ ├── canny_output.png │ ├── contours_analysis.png │ ├── opencv_052.cpp │ ├── opencv_052.py │ └── test.png ├── code_053 │ ├── contours_analysis.png │ ├── opencv_053.cpp │ ├── opencv_053.py │ └── test.png ├── code_054 │ ├── canny_output.png │ ├── contours_analysis.png │ ├── opencv_054.cpp │ ├── opencv_054.py │ └── test.png ├── code_055 │ ├── contours_analysis.png │ ├── opencv_055.cpp │ ├── opencv_055.py │ └── test.jpg ├── code_056 │ ├── canny_output.png │ ├── contours_analysis.png │ ├── opencv_056.cpp │ ├── opencv_056.py │ └── test.png ├── code_057 │ ├── contours_analysis.png │ ├── opencv_057.cpp │ ├── opencv_057.py │ └── test.png ├── code_058 │ ├── opencv_058.cpp │ ├── opencv_058.py │ └── test.png ├── code_059 │ ├── Mat.png │ ├── canny_output.png │ ├── contours_analysis.png │ ├── opencv_059.cpp │ ├── opencv_059.py │ └── test.png ├── code_060 │ ├── Mat.png │ ├── canny_output.png │ ├── contours_analysis.png │ ├── opencv_060.cpp │ └── opencv_060.py ├── code_061 │ ├── contours_analysis.png │ ├── opencv_061.cpp │ ├── opencv_061.py │ └── test.png ├── code_062 │ ├── dilate.png │ ├── erode.png │ ├── opencv_062.cpp │ ├── opencv_062.py │ └── test.png ├── code_063 │ ├── dilate.png │ ├── erode.png │ ├── opencv_063.cpp │ ├── opencv_063.py │ └── test.png ├── code_064 │ ├── opencv_064.cpp │ ├── opencv_064.py │ └── test.png ├── code_065 │ ├── binary1.png │ ├── binary2.png │ ├── opencv_065.cpp │ ├── opencv_065.py │ └── test.png ├── code_066 │ ├── close.png │ ├── opencv_066.cpp │ ├── opencv_066.py │ └── test.png ├── code_067 │ ├── binary2.png │ ├── opencv_067.cpp │ ├── opencv_067.py │ └── test.png ├── code_068 │ ├── opencv_068.cpp │ ├── opencv_068.py │ └── test.png ├── code_069 │ ├── external.png │ ├── gradient.png │ ├── interal.png │ ├── opencv_069.py │ ├── opencv_69.cpp │ └── test.png ├── code_070 │ ├── opencv_070.cpp │ ├── opencv_070.py │ └── test.png ├── code_071 │ ├── binary2.png │ ├── opencv_071.cpp │ ├── opencv_071.py │ └── test.png ├── code_072 │ ├── binary2.png │ ├── opencv_072.cpp │ ├── opencv_072.py │ └── test.jpg ├── code_073 │ ├── binary2.png │ ├── mask1.png │ ├── mask2.png │ ├── mask3.png │ ├── mask4.png │ ├── opencv_073.cpp │ ├── opencv_073.py │ └── test.jpg ├── code_074 │ ├── opencv_074.cpp │ ├── opencv_074.py │ ├── output.png │ ├── result.png │ └── test.png ├── code_075 │ ├── mask.png │ ├── opencv_075.cpp │ ├── opencv_075.py │ ├── result.png │ └── test.png ├── code_076 │ ├── binary.png │ ├── opencv_076.cpp │ ├── opencv_076.py │ ├── result.png │ └── test.jpg ├── code_077 │ ├── opencv_077.cpp │ └── opencv_077.py ├── code_078 │ ├── opencv_078.cpp │ └── opencv_078.py ├── code_079 │ ├── opencv_079.cpp │ └── opencv_079.py ├── code_080 │ ├── mask.png │ ├── opencv_080.cpp │ └── opencv_080.py └── test.png ├── README.md └── object-tracker.py /OpenCV_learn/Mat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/Mat.png -------------------------------------------------------------------------------- /OpenCV_learn/code_001/001.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | // Mat src = imread("D:/vcprojects/images/test.png"); 9 | Mat src = imread("./test.png", IMREAD_GRAYSCALE); 10 | 11 | if (src.empty()) { 12 | printf("could not load image...\n"); 13 | return -1; 14 | } 15 | namedWindow("input", CV_WINDOW_AUTOSIZE); 16 | imshow("input", src); 17 | 18 | waitKey(0); 19 | return 0; 20 | } 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /OpenCV_learn/code_001/opencv_001.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | src = cv.imread("./test.png") 4 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 5 | cv.imshow("input", src) 6 | cv.waitKey(0) 7 | cv.destroyAllWindows() 8 | 9 | 10 | -------------------------------------------------------------------------------- /OpenCV_learn/code_001/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_001/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_002/gray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_002/gray.png -------------------------------------------------------------------------------- /OpenCV_learn/code_002/opencv_002.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat gray; 17 | cvtColor(src, gray, COLOR_BGR2GRAY); 18 | imwrite("./gray.png", gray); 19 | 20 | waitKey(0); 21 | return 0; 22 | } 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /OpenCV_learn/code_002/opencv_002.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | src = cv.imread("./test.png") 4 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 5 | cv.imshow("input", src) 6 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 7 | cv.imwrite('gray.png', gray) 8 | cv.imshow("gray", gray) 9 | cv.waitKey(0) 10 | cv.destroyAllWindows() 11 | 12 | -------------------------------------------------------------------------------- /OpenCV_learn/code_002/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_002/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_003/opencv_003.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | // �������� - ��¡ 17 | Mat m1 = src.clone(); 18 | 19 | // ���� 20 | Mat m2; 21 | src.copyTo(m2); 22 | 23 | // ��ֵ�� 24 | Mat m3 = src; 25 | 26 | // �����հ�ͼ�� 27 | Mat m4 = Mat::zeros(src.size(), src.type()); 28 | Mat m5 = Mat::zeros(Size(512, 512), CV_8UC3); 29 | Mat m6 = Mat::ones(Size(512, 512), CV_8UC3); 30 | 31 | Mat kernel = (Mat_(3, 3) << 0, -1, 0, 32 | -1, 5, -1, 33 | 0, -1, 0); 34 | 35 | waitKey(0); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /OpenCV_learn/code_003/opencv_003.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 克隆图像 9 | m1 = np.copy(src) 10 | 11 | # 赋值 12 | m2 = src 13 | src[100:200,200:300,:] = 255 14 | cv.imshow("m2",m2) 15 | 16 | m3 = np.zeros(src.shape, src.dtype) 17 | cv.imshow("m3", m3) 18 | 19 | m4 = np.zeros([512,512], np.uint8) 20 | # m4[:,:] =127 try to give gray value 127 21 | cv.imshow("m4", m4) 22 | 23 | m5 = np.ones(shape=[512,512,3], dtype=np.uint8) 24 | m5[:,:,0] = 255 25 | cv.imshow("m5", m5) 26 | 27 | 28 | 29 | 30 | cv.waitKey(0) 31 | cv.destroyAllWindows() 32 | 33 | -------------------------------------------------------------------------------- /OpenCV_learn/code_003/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_003/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_004/code_004.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | // ֱ�Ӷ�ȡͼ������ 17 | int height = src.rows; 18 | int width = src.cols; 19 | int ch = src.channels(); 20 | for (int c = 0; c < ch; c++) { 21 | for (int row = 0; row < height; row++) { 22 | for (int col = 0; col < width; col++) { 23 | if (ch == 3) { 24 | Vec3b bgr = src.at(row, col); 25 | bgr[0] = 255 - bgr[0]; 26 | bgr[1] = 255 - bgr[1]; 27 | bgr[2] = 255 - bgr[2]; 28 | src.at(row, col) = bgr; 29 | } else if(ch == 1) { 30 | int gray = src.at(row, col); 31 | src.at(row, col) = 255 - gray; 32 | } 33 | } 34 | } 35 | } 36 | imshow("output", src); 37 | 38 | // ָ���ȡ 39 | Mat result = Mat::zeros(src.size(), src.type()); 40 | int blue = 0, green = 0, red = 0; 41 | int gray; 42 | for (int c = 0; c < ch; c++) { 43 | for (int row = 0; row < height; row++) { 44 | uchar* curr_row = src.ptr(row); 45 | uchar* result_row = result.ptr(row); 46 | for (int col = 0; col < width; col++) { 47 | if (ch == 3) { 48 | blue = *curr_row++; 49 | green = *curr_row++; 50 | red = *curr_row++; 51 | 52 | *result_row++ = blue; 53 | *result_row++ = green; 54 | *result_row++ = red; 55 | } 56 | else if (ch == 1) { 57 | gray = *curr_row++; 58 | *result_row++ = gray; 59 | } 60 | } 61 | } 62 | } 63 | imshow("result", result); 64 | 65 | waitKey(0); 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /OpenCV_learn/code_004/opencv_004.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | src = cv.imread("./test.png") 4 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 5 | cv.imshow("input", src) 6 | h, w, ch = src.shape 7 | print("h , w, ch", h, w, ch) 8 | for row in range(h): 9 | for col in range(w): 10 | b, g, r = src[row, col] 11 | b = 255 - b 12 | g = 255 - g 13 | r = 255 - r 14 | src[row, col] = [b, g, r] 15 | cv.imshow("output", src) 16 | 17 | cv.waitKey(0) 18 | cv.destroyAllWindows() 19 | 20 | -------------------------------------------------------------------------------- /OpenCV_learn/code_004/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_004/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_005/code_005.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src1 = imread("./test0.jpg"); 9 | Mat src2 = imread("./test1.jpg"); 10 | if (src1.empty() || src2.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", CV_WINDOW_AUTOSIZE); 15 | imshow("input1", src1); 16 | imshow("input2", src2); 17 | int height = src1.rows; 18 | int width = src1.cols; 19 | 20 | int b1 = 0, g1 = 0, r1 = 0; 21 | int b2 = 0, g2 = 0, r2 = 0; 22 | int b = 0, g = 0, r = 0; 23 | Mat result = Mat::zeros(src1.size(), src1.type()); 24 | for (int row = 0; row < height; row++) { 25 | for (int col = 0; col < width; col++) { 26 | b1 = src1.at(row, col)[0]; 27 | g1 = src1.at(row, col)[1]; 28 | r1 = src1.at(row, col)[2]; 29 | 30 | b2 = src2.at(row, col)[0]; 31 | g2 = src2.at(row, col)[1]; 32 | r2 = src2.at(row, col)[2]; 33 | 34 | result.at(row, col)[0] = saturate_cast(b1 + b2); 35 | result.at(row, col)[1] = saturate_cast(g1 + g2); 36 | result.at(row, col)[2] = saturate_cast(r1 + r2); 37 | } 38 | } 39 | imshow("output", result); 40 | 41 | Mat add_result = Mat::zeros(src1.size(), src1.type()); 42 | add(src1, src2, add_result); 43 | imshow("add_result", add_result); 44 | 45 | Mat sub_result = Mat::zeros(src1.size(), src1.type()); 46 | subtract(src1, src2, sub_result); 47 | imshow("sub_result", sub_result); 48 | 49 | Mat mul_result = Mat::zeros(src1.size(), src1.type()); 50 | multiply(src1, src2, mul_result); 51 | imshow("mul_result", mul_result); 52 | 53 | Mat div_result = Mat::zeros(src1.size(), src1.type()); 54 | divide(src1, src2, div_result); 55 | imshow("div_result", div_result); 56 | 57 | waitKey(0); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /OpenCV_learn/code_005/opencv_005.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src1 = cv.imread("./test0.jpg") 5 | src2 = cv.imread("./test1.jpg") 6 | cv.imshow("input1", src1) 7 | cv.imshow("input2", src2) 8 | h, w, ch = src1.shape 9 | print("h , w, ch", h, w, ch) 10 | 11 | add_result = np.zeros(src1.shape, src1.dtype) 12 | cv.add(src1, src2, add_result) 13 | cv.imshow("add_result", add_result) 14 | 15 | sub_result = np.zeros(src1.shape, src1.dtype) 16 | cv.subtract(src1, src2, sub_result) 17 | cv.imshow("sub_result", sub_result) 18 | 19 | mul_result = np.zeros(src1.shape, src1.dtype) 20 | cv.multiply(src1, src2, mul_result) 21 | cv.imshow("mul_result", mul_result) 22 | 23 | div_result = np.zeros(src1.shape, src1.dtype) 24 | cv.divide(src1, src2, div_result) 25 | cv.imshow("div_result", div_result) 26 | 27 | cv.waitKey(0) 28 | cv.destroyAllWindows() 29 | 30 | -------------------------------------------------------------------------------- /OpenCV_learn/code_005/test0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_005/test0.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_005/test1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_005/test1.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_006/code_006.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | void customColorMap(Mat &image); 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("cos.jpg"); 10 | if (src.empty()) 11 | { 12 | printf("could not load image...\n"); 13 | return -1; 14 | } 15 | Mat gray, dst; 16 | // ʹ��LUT 17 | applyColorMap(src, dst, COLORMAP_SUMMER); 18 | // ��ʾ��� 19 | imshow("colorMap", dst); 20 | 21 | cvtColor(src, gray, COLOR_BGR2GRAY); 22 | imshow("gray", gray); 23 | customColorMap(gray); 24 | 25 | waitKey(0); 26 | return 0; 27 | } 28 | 29 | void customColorMap(Mat &image) { 30 | int lut[256]; 31 | for (int i = 0; i < 256; i++) { 32 | if (i < 127) 33 | lut[i] = 0; 34 | else 35 | lut[i] = 255; 36 | } 37 | 38 | int h = image.rows; 39 | int w = image.cols; 40 | for (int row = 0; row < h; row++) { 41 | for (int col = 0; col < w; col++) { 42 | int pv = image.at(row, col); 43 | image.at(row, col) = lut[pv]; 44 | } 45 | } 46 | imshow("lut demo", image); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /OpenCV_learn/code_006/opencv_006.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | src = cv.imread("test1.png") 4 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 5 | cv.imshow("input", src) 6 | dst = cv.applyColorMap(src, cv.COLORMAP_COOL) 7 | cv.imshow("output", dst) 8 | 9 | # 伪色彩 10 | image = cv.imread("test0.jpg") 11 | color_image = cv.applyColorMap(image, cv.COLORMAP_JET) 12 | cv.imshow("image", image) 13 | cv.imshow("color_image", color_image) 14 | cv.waitKey(0) 15 | cv.destroyAllWindows() 16 | 17 | 18 | -------------------------------------------------------------------------------- /OpenCV_learn/code_006/test0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_006/test0.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_006/test1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_006/test1.png -------------------------------------------------------------------------------- /OpenCV_learn/code_007/Mat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_007/Mat.png -------------------------------------------------------------------------------- /OpenCV_learn/code_007/opencv_007.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | // create image one 10 | Mat src1 = Mat::zeros(Size(400, 400), CV_8UC3); 11 | Rect rect(100, 100, 100, 100); 12 | src1(rect) = Scalar(0, 0, 255); 13 | imshow("input1", src1); 14 | printf("create first image...\n"); 15 | 16 | // create image two 17 | Mat src2 = Mat::zeros(Size(400, 400), CV_8UC3); 18 | rect.x = 150; 19 | rect.y = 150; 20 | src2(rect) = Scalar(0, 0, 255); 21 | imshow("input2", src2); 22 | printf("create second image...\n"); 23 | 24 | // �߼����� 25 | Mat dst1, dst2, dst3; 26 | bitwise_and(src1, src2, dst1); 27 | bitwise_xor(src1, src2, dst2); 28 | bitwise_or(src1, src2, dst3); 29 | 30 | // show results 31 | imshow("dst1", dst1); 32 | imshow("dst2", dst2); 33 | imshow("dst3", dst3); 34 | 35 | Mat src = imread("./test.png"); 36 | namedWindow("input", WINDOW_AUTOSIZE); 37 | imshow("input", src); 38 | // ȡ������ 39 | Mat dst; 40 | bitwise_not(src, dst); 41 | imshow("dst", dst); 42 | 43 | waitKey(0); 44 | return 0; 45 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_007/opencv_007.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | # create image one 5 | src1 = np.zeros(shape=[400, 400, 3], dtype=np.uint8) 6 | src1[100:200, 100:200, 1] = 255 7 | src1[100:200, 100:200, 2] = 255 8 | cv.imshow("input1", src1) 9 | # create image two 10 | src2 = np.zeros(shape=[400, 400, 3], dtype=np.uint8) 11 | src2[150:250, 150:250, 2] = 255 12 | cv.imshow("input2", src2) 13 | 14 | dst1 = cv.bitwise_and(src1, src2) 15 | dst2 = cv.bitwise_xor(src1, src2) 16 | dst3 = cv.bitwise_or(src1, src2) 17 | 18 | 19 | cv.imshow("dst1", dst1) 20 | cv.imshow("dst2", dst2) 21 | cv.imshow("dst3", dst3) 22 | 23 | src = cv.imread("./test.png") 24 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 25 | cv.imshow("input", src) 26 | dst = cv.bitwise_not(src) 27 | cv.imshow("dst", dst) 28 | 29 | cv.waitKey(0) 30 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_007/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_007/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_008/opencv_008.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("/test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | vector mv; 18 | Mat dst1, dst2, dst3; 19 | // ��ɫͨ��Ϊ�� 20 | split(src, mv); 21 | mv[0] = Scalar(0); 22 | merge(mv, dst1); 23 | imshow("output1", dst1); 24 | 25 | // ��ɫͨ��Ϊ�� 26 | split(src, mv); 27 | mv[1] = Scalar(0); 28 | merge(mv, dst2); 29 | imshow("output2", dst2); 30 | 31 | // ��ɫͨ��Ϊ�� 32 | split(src, mv); 33 | mv[2] = Scalar(0); 34 | merge(mv, dst3); 35 | imshow("output3", dst3); 36 | 37 | waitKey(0); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_008/opencv_008.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | src = cv.imread("./test.png") 4 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 5 | cv.imshow("input", src) 6 | 7 | # 蓝色通道为零 8 | mv = cv.split(src) 9 | mv[0][:, :] = 0 10 | dst1 = cv.merge(mv) 11 | cv.imshow("output1", dst1) 12 | 13 | # 绿色通道为零 14 | mv = cv.split(src) 15 | mv[1][:, :] = 0 16 | dst2 = cv.merge(mv) 17 | cv.imshow("output2", dst2) 18 | 19 | # 红色通道为零 20 | mv = cv.split(src) 21 | mv[2][:, :] = 0 22 | dst3 = cv.merge(mv) 23 | cv.imshow("output3", dst3) 24 | 25 | cv.mixChannels(src, dst3, [2, 0]) 26 | cv.imshow("output4", dst3) 27 | 28 | cv.waitKey(0) 29 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_008/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_008/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_009/opencv_009.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("test.jpg"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // RGB to HSV 18 | Mat hsv; 19 | cvtColor(src, hsv, COLOR_BGR2HSV); 20 | imshow("hsv", hsv); 21 | 22 | // RGB to YUV 23 | Mat yuv; 24 | cvtColor(src, yuv, COLOR_BGR2YUV); 25 | imshow("yuv", yuv); 26 | 27 | // RGB to YUV 28 | Mat ycrcb; 29 | cvtColor(src, ycrcb, COLOR_BGR2YCrCb); 30 | imshow("ycrcb", ycrcb); 31 | 32 | Mat src2 = imread("test.png"); 33 | imshow("src2", src2); 34 | cvtColor(src2, hsv, COLOR_BGR2HSV); 35 | Mat mask; 36 | inRange(hsv, Scalar(35, 43, 46), Scalar(99, 255, 255), mask); 37 | imshow("mask", mask); 38 | 39 | waitKey(0); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_009/opencv_009.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | src = cv.imread("test.jpg") 4 | cv.namedWindow("rgb", cv.WINDOW_AUTOSIZE) 5 | cv.imshow("rgb", src) 6 | 7 | # RGB to HSV 8 | hsv = cv.cvtColor(src, cv.COLOR_BGR2HSV) 9 | cv.imshow("hsv", hsv) 10 | 11 | # RGB to YUV 12 | yuv = cv.cvtColor(src, cv.COLOR_BGR2YUV) 13 | cv.imshow("yuv", yuv) 14 | 15 | # RGB to YUV 16 | ycrcb = cv.cvtColor(src, cv.COLOR_BGR2YCrCb) 17 | cv.imshow("ycrcb", ycrcb) 18 | 19 | src2 = cv.imread("test.png"); 20 | cv.imshow("src2", src2) 21 | hsv = cv.cvtColor(src2, cv.COLOR_BGR2HSV) 22 | mask = cv.inRange(hsv, (35, 43, 46), (99, 255, 255)) 23 | cv.imshow("mask", mask) 24 | 25 | cv.waitKey(0) 26 | cv.destroyAllWindows() 27 | 28 | 29 | -------------------------------------------------------------------------------- /OpenCV_learn/code_009/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_009/test.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_009/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_009/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_010/code_010.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png", IMREAD_GRAYSCALE); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | double minVal; double maxVal; Point minLoc; Point maxLoc; 18 | minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc, Mat()); 19 | printf("min: %.2f, max: %.2f \n", minVal, maxVal); 20 | printf("min loc: (%d, %d) \n", minLoc.x, minLoc.y); 21 | printf("max loc: (%d, %d)\n", maxLoc.x, maxLoc.y); 22 | 23 | // ��ɫͼ�� ��ͨ���� ��ֵ�뷽�� 24 | src = imread("./test.png"); 25 | Mat means, stddev; 26 | meanStdDev(src, means, stddev); 27 | printf("blue channel->> mean: %.2f, stddev: %.2f\n", means.at(0, 0), stddev.at(0, 0)); 28 | printf("green channel->> mean: %.2f, stddev: %.2f\n", means.at(1, 0), stddev.at(1, 0)); 29 | printf("red channel->> mean: %.2f, stddev: %.2f\n", means.at(2, 0), stddev.at(2, 0)); 30 | 31 | 32 | waitKey(0); 33 | return 0; 34 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_010/opencv_010.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png", cv.IMREAD_GRAYSCALE) 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | min, max, minLoc, maxLoc = cv.minMaxLoc(src) 9 | print("min: %.2f, max: %.2f"% (min, max)) 10 | print("min loc: ", minLoc) 11 | print("max loc: ", maxLoc) 12 | 13 | means, stddev = cv.meanStdDev(src) 14 | print("mean: %.2f, stddev: %.2f"% (means, stddev)) 15 | src[np.where(src < means)] = 0 16 | src[np.where(src > means)] = 255 17 | cv.imshow("binary", src) 18 | 19 | cv.waitKey(0) 20 | cv.destroyAllWindows() 21 | 22 | 23 | -------------------------------------------------------------------------------- /OpenCV_learn/code_010/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_010/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_011/opencv_011.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | Mat gray, gray_f; 18 | cvtColor(src, gray, COLOR_BGR2GRAY); 19 | 20 | // ת��Ϊ�������������� 21 | gray.convertTo(gray, CV_32F); 22 | 23 | // scale and shift by NORM_MINMAX 24 | Mat dst = Mat::zeros(gray.size(), CV_32FC1); 25 | normalize(gray, dst, 1.0, 0, NORM_MINMAX); 26 | Mat result = dst * 255; 27 | result.convertTo(dst, CV_8UC1); 28 | imshow("NORM_MINMAX", dst); 29 | 30 | // scale and shift by NORM_INF 31 | normalize(gray, dst, 1.0, 0, NORM_INF); 32 | result = dst * 255; 33 | result.convertTo(dst, CV_8UC1); 34 | imshow("NORM_INF", dst); 35 | 36 | // scale and shift by NORM_L1 37 | normalize(gray, dst, 1.0, 0, NORM_L1); 38 | result = dst * 10000000; 39 | result.convertTo(dst, CV_8UC1); 40 | imshow("NORM_L1", dst); 41 | 42 | // scale and shift by NORM_L2 43 | normalize(gray, dst, 1.0, 0, NORM_L2); 44 | result = dst * 10000; 45 | result.convertTo(dst, CV_8UC1); 46 | imshow("NORM_L2", dst); 47 | 48 | waitKey(0); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_011/opencv_011.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 8 | 9 | # 转换为浮点数类型数组 10 | gray = np.float32(gray) 11 | print(gray) 12 | 13 | # scale and shift by NORM_MINMAX 14 | dst = np.zeros(gray.shape, dtype=np.float32) 15 | cv.normalize(gray, dst=dst, alpha=0, beta=1.0, norm_type=cv.NORM_MINMAX) 16 | print(dst) 17 | cv.imshow("NORM_MINMAX", np.uint8(dst*255)) 18 | 19 | # scale and shift by NORM_INF 20 | dst = np.zeros(gray.shape, dtype=np.float32) 21 | cv.normalize(gray, dst=dst, alpha=1.0, beta=0, norm_type=cv.NORM_INF) 22 | print(dst) 23 | cv.imshow("NORM_INF", np.uint8(dst*255)) 24 | 25 | # scale and shift by NORM_L1 26 | dst = np.zeros(gray.shape, dtype=np.float32) 27 | cv.normalize(gray, dst=dst, alpha=1.0, beta=0, norm_type=cv.NORM_L1) 28 | print(dst) 29 | cv.imshow("NORM_L1", np.uint8(dst*10000000)) 30 | 31 | # scale and shift by NORM_L2 32 | dst = np.zeros(gray.shape, dtype=np.float32) 33 | cv.normalize(gray, dst=dst, alpha=1.0, beta=0, norm_type=cv.NORM_L2) 34 | print(dst) 35 | cv.imshow("NORM_L2", np.uint8(dst*10000)) 36 | 37 | cv.waitKey(0) 38 | cv.destroyAllWindows() 39 | 40 | 41 | -------------------------------------------------------------------------------- /OpenCV_learn/code_011/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_011/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_012/opencv_012.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, char** argv) { 8 | // ������ͷ 9 | // VideoCapture capture(0); 10 | 11 | // ���ļ� 12 | VideoCapture capture; 13 | capture.open("./test.avi"); 14 | if (!capture.isOpened()) { 15 | printf("could not read this video file...\n"); 16 | return -1; 17 | } 18 | Size S = Size((int)capture.get(CV_CAP_PROP_FRAME_WIDTH), 19 | (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT)); 20 | int fps = capture.get(CV_CAP_PROP_FPS); 21 | printf("current fps : %d \n", fps); 22 | VideoWriter writer("./test_cp.mp4", CV_FOURCC('D', 'I', 'V', 'X'), fps, S, true); 23 | 24 | Mat frame; 25 | namedWindow("camera-demo", CV_WINDOW_AUTOSIZE); 26 | while (capture.read(frame)) { 27 | imshow("camera-demo", frame); 28 | writer.write(frame); 29 | char c = waitKey(50); 30 | if (c == 27) { 31 | break; 32 | } 33 | } 34 | capture.release(); 35 | writer.release(); 36 | waitKey(0); 37 | return 0; 38 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_012/opencv_012.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | capture = cv.VideoCapture("./test.avi") 6 | # capture = cv.VideoCapture(0) 打开摄像头 7 | height = capture.get(cv.CAP_PROP_FRAME_HEIGHT) 8 | width = capture.get(cv.CAP_PROP_FRAME_WIDTH) 9 | count = capture.get(cv.CAP_PROP_FRAME_COUNT) 10 | fps = capture.get(cv.CAP_PROP_FPS) 11 | print(height, width, count, fps) 12 | out = cv.VideoWriter("./test_cp.avi", cv.VideoWriter_fourcc('D', 'I', 'V', 'X'), 15, 13 | (np.int(width), np.int(height)), True) 14 | while True: 15 | ret, frame = capture.read() 16 | if ret is True: 17 | cv.imshow("video-input", frame) 18 | out.write(frame) 19 | c = cv.waitKey(50) 20 | if c == 27: # ESC 21 | break 22 | else: 23 | break 24 | 25 | capture.release() 26 | out.release() 27 | 28 | 29 | -------------------------------------------------------------------------------- /OpenCV_learn/code_013/opencv_013.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | imshow("input", src); 14 | 15 | Mat dst; 16 | // X Flip ��Ӱ 17 | flip(src, dst, 0); 18 | imshow("x-flip", dst); 19 | 20 | // Y Flip ���� 21 | flip(src, dst, 1); 22 | imshow("y-flip", dst); 23 | 24 | // XY Flip �Խ� 25 | flip(src, dst, -1); 26 | imshow("xy-flip", dst); 27 | 28 | waitKey(0); 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /OpenCV_learn/code_013/opencv_013.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # X Flip 倒影 9 | dst1 = cv.flip(src, 0); 10 | cv.imshow("x-flip", dst1); 11 | 12 | # Y Flip 镜像 13 | dst2 = cv.flip(src, 1); 14 | cv.imshow("y-flip", dst2); 15 | 16 | # XY Flip 对角 17 | dst3 = cv.flip(src, -1); 18 | cv.imshow("xy-flip", dst3); 19 | 20 | # custom y-flip 21 | h, w, ch = src.shape 22 | dst = np.zeros(src.shape, src.dtype) 23 | for row in range(h): 24 | for col in range(w): 25 | b, g, r = src[row, col] 26 | dst[row, w - col - 1] = [b, g, r] 27 | cv.imshow("custom-y-flip", dst) 28 | 29 | cv.waitKey(0) 30 | cv.destroyAllWindows() 31 | 32 | 33 | -------------------------------------------------------------------------------- /OpenCV_learn/code_013/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_013/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_014/opencv_014.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | imshow("input", src); 14 | 15 | int h = src.rows; 16 | int w = src.cols; 17 | float fx = 0.0, fy = 0.0; 18 | Mat dst = Mat::zeros(src.size(), src.type()); 19 | resize(src, dst, Size(w * 2, h * 2), fx = 0, fy = 0, INTER_NEAREST); 20 | imshow("INTER_NEAREST", dst); 21 | 22 | resize(src, dst, Size(w * 2, h * 2), fx = 0, fy = 0, INTER_LINEAR); 23 | imshow("INTER_LINEAR", dst); 24 | 25 | resize(src, dst, Size(w * 2, h * 2), fx = 0, fy = 0, INTER_CUBIC); 26 | imshow("INTER_CUBIC", dst); 27 | 28 | resize(src, dst, Size(w * 2, h * 2), fx = 0, fy = 0, INTER_LANCZOS4); 29 | imshow("INTER_LANCZOS4", dst); 30 | 31 | waitKey(0); 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /OpenCV_learn/code_014/opencv_014.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | src = cv.imread("./test.png") 4 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 5 | cv.imshow("input", src) 6 | 7 | h, w = src.shape[:2] 8 | print(h, w) 9 | dst = cv.resize(src, (w*2, h*2), fx=0.75, fy=0.75, interpolation=cv.INTER_NEAREST) 10 | cv.imshow("INTER_NEAREST", dst) 11 | 12 | dst = cv.resize(src, (w*2, h*2), interpolation=cv.INTER_LINEAR) 13 | cv.imshow("INTER_LINEAR", dst) 14 | 15 | dst = cv.resize(src, (w*2, h*2), interpolation=cv.INTER_CUBIC) 16 | cv.imshow("INTER_CUBIC", dst) 17 | 18 | dst = cv.resize(src, (w*2, h*2), interpolation=cv.INTER_LANCZOS4) 19 | cv.imshow("INTER_LANCZOS4", dst) 20 | 21 | # cv.warpAffine() 22 | 23 | cv.waitKey(0) 24 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_014/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_014/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_015/opencv_015.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, char** argv) { 8 | Mat image = Mat::zeros(Size(512, 512), CV_8UC3); 9 | Rect rect(100, 100, 200, 200); 10 | rectangle(image, rect, Scalar(255, 0, 0), 2, LINE_8, 0); 11 | circle(image, Point(256, 256), 50, Scalar(0, 0, 255), 2, LINE_8, 0); 12 | ellipse(image, Point(256, 256), Size(150, 50), 360, 0, 360, Scalar(0, 255, 0), 2, LINE_8, 0); 13 | imshow("image", image); 14 | waitKey(0); 15 | 16 | RNG rng(0xFFFFFF); 17 | image.setTo(Scalar(0, 0, 0)); 18 | 19 | for (int i = 0; i < 100000; i++) { 20 | // image.setTo(Scalar(0, 0, 0)); 21 | int x1 = rng.uniform(0, 512); 22 | int y1 = rng.uniform(0, 512); 23 | int x2 = rng.uniform(0, 512); 24 | int y2 = rng.uniform(0, 512); 25 | 26 | int b = rng.uniform(0, 256); 27 | int g = rng.uniform(0, 256); 28 | int r = rng.uniform(0, 256); 29 | line(image, Point(x1, y1), Point(x2, y2), Scalar(b, g, r), 1, LINE_AA, 0); 30 | rect.x = x1; 31 | rect.y = y1; 32 | rect.width = x2 - x1; 33 | rect.height = y2 - y1; 34 | // rectangle(image, rect, Scalar(b, g, r), 1, LINE_AA, 0); 35 | imshow("image", image); 36 | char c = waitKey(20); 37 | if (c == 27) 38 | break; 39 | 40 | imshow("image", image); 41 | } 42 | waitKey(0); 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /OpenCV_learn/code_015/opencv_015.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | image = np.zeros((512, 512, 3), dtype=np.uint8) 5 | 6 | cv.rectangle(image, (100, 100), (300, 300), (255, 0, 0), 2, cv.LINE_8, 0) 7 | cv.circle(image, (256, 256), 50, (0, 0, 255), 2, cv.LINE_8, 0) 8 | cv.ellipse(image, (256, 256), (150, 50), 360, 0, 360, (0, 255, 0), 2, cv.LINE_8, 0) 9 | cv.imshow("image", image) 10 | cv.waitKey(0) 11 | 12 | for i in range(100000): 13 | image[:,:,:]= 0 14 | x1 = np.random.rand() * 512 15 | y1 = np.random.rand() * 512 16 | x2 = np.random.rand() * 512 17 | y2 = np.random.rand() * 512 18 | 19 | b = np.random.randint(0, 256) 20 | g = np.random.randint(0, 256) 21 | r = np.random.randint(0, 256) 22 | # cv.line(image, (np.int(x1), np.int(y1)), (np.int(x2), np.int(y2)), (b, g, r), 4, cv.LINE_8, 0) 23 | cv.rectangle(image, (np.int(x1), np.int(y1)), (np.int(x2), np.int(y2)), (b, g, r), 1, cv.LINE_8, 0) 24 | cv.imshow("image", image) 25 | c = cv.waitKey(20) 26 | if c == 27: 27 | break # ESC不 28 | 29 | 30 | cv.imshow("image", image) 31 | cv.waitKey(0) 32 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_016/opencv_016.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | namedWindow("input", WINDOW_AUTOSIZE); 10 | imshow("input", src); 11 | int h = src.rows; 12 | int w = src.cols; 13 | 14 | // ��ȡROI 15 | int cy = h / 2; 16 | int cx = w / 2; 17 | Rect rect(cx - 100, cy - 100, 200, 200); 18 | Mat roi = src(rect); 19 | imshow("roi", roi); 20 | 21 | Mat image = roi.clone(); 22 | // modify ROI 23 | roi.setTo(Scalar(255, 0, 0)); 24 | imshow("result", src); 25 | 26 | // modify copy roi 27 | image.setTo(Scalar(0, 0, 255)); 28 | imshow("result", src); 29 | imshow("copy roi", image); 30 | 31 | // example with ROI - generate mask 32 | Mat src2 = imread("./test.png"); 33 | imshow("src2", src2); 34 | Mat hsv, mask; 35 | cvtColor(src2, hsv, COLOR_BGR2HSV); 36 | inRange(hsv, Scalar(35, 43, 46), Scalar(99, 255, 255), mask); 37 | imshow("mask", mask); 38 | 39 | // extract person ROI 40 | Mat person; 41 | bitwise_not(mask, mask); 42 | bitwise_and(src2, src2, person, mask); 43 | imshow("person", person); 44 | 45 | // generate background 46 | Mat result = Mat::zeros(src2.size(), src2.type()); 47 | result.setTo(Scalar(255, 0, 0)); 48 | 49 | // combine background + person 50 | Mat dst; 51 | bitwise_not(mask, mask); 52 | bitwise_or(person, result, dst, mask); 53 | add(dst, person, dst); 54 | 55 | imshow("dst", dst); 56 | waitKey(0); 57 | return 0; 58 | } 59 | 60 | -------------------------------------------------------------------------------- /OpenCV_learn/code_016/opencv_016.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | h, w = src.shape[:2] 8 | 9 | # 获取ROI 10 | cy = h//2 11 | cx = w//2 12 | roi = src[cy-100:cy+100,cx-100:cx+100,:] 13 | cv.imshow("roi", roi) 14 | 15 | # copy ROI 16 | image = np.copy(roi) 17 | 18 | # modify ROI 19 | roi[:, :, 0] = 0 20 | cv.imshow("result", src) 21 | 22 | # modify copy roi 23 | image[:, :, 2] = 0 24 | cv.imshow("result", src) 25 | cv.imshow("copy roi", image) 26 | 27 | # example with ROI - generate mask 28 | src2 = cv.imread("./test.png") 29 | cv.imshow("src2", src2) 30 | hsv = cv.cvtColor(src2, cv.COLOR_BGR2HSV) 31 | mask = cv.inRange(hsv, (35, 43, 46), (99, 255, 255)) 32 | 33 | # extract person ROI 34 | mask = cv.bitwise_not(mask) 35 | person = cv.bitwise_and(src2, src2, mask=mask) 36 | 37 | # generate background 38 | result = np.zeros(src2.shape, src2.dtype) 39 | result[:,:,0] = 255 40 | 41 | # combine background + person 42 | mask = cv.bitwise_not(mask) 43 | dst = cv.bitwise_or(person, result, mask=mask) 44 | dst = cv.add(dst, person) 45 | 46 | cv.imshow("dst", dst) 47 | 48 | cv.waitKey(0) 49 | cv.destroyAllWindows() 50 | 51 | 52 | -------------------------------------------------------------------------------- /OpenCV_learn/code_016/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_016/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_017/opencv_017.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def custom_hist(gray): 7 | h, w = gray.shape 8 | hist = np.zeros([256], dtype=np.int32) 9 | for row in range(h): 10 | for col in range(w): 11 | pv = gray[row, col] 12 | hist[pv] += 1 13 | 14 | y_pos = np.arange(0, 256, 1, dtype=np.int32) 15 | plt.bar(y_pos, hist, align='center', color='r', alpha=0.5) 16 | plt.xticks(y_pos, y_pos) 17 | plt.ylabel('Frequency') 18 | plt.title('Histogram') 19 | 20 | # plt.plot(hist, color='r') 21 | # plt.xlim([0, 256]) 22 | plt.show() 23 | 24 | 25 | def image_hist(image): 26 | cv.imshow("input", image) 27 | color = ('blue', 'green', 'red') 28 | for i, color in enumerate(color): 29 | hist = cv.calcHist([image], [i], None, [256], [0, 256]) 30 | plt.plot(hist, color=color) 31 | plt.xlim([0, 256]) 32 | plt.show() 33 | 34 | 35 | src = cv.imread("./test.png") 36 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 37 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 38 | cv.imshow("input", gray) 39 | # custom_hist(gray) 40 | image_hist(src) 41 | cv.waitKey(0) 42 | cv.destroyAllWindows() 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /OpenCV_learn/code_017/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_017/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_018/opencv_018.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", CV_WINDOW_AUTOSIZE); 15 | Mat gray, dst; 16 | cvtColor(src, gray, COLOR_BGR2GRAY); 17 | imshow("input", gray); 18 | equalizeHist(gray, dst); 19 | imshow("eq", dst); 20 | 21 | waitKey(0); 22 | return 0; 23 | } 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /OpenCV_learn/code_018/opencv_018.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def custom_hist(gray): 7 | h, w = gray.shape 8 | hist = np.zeros([256], dtype=np.int32) 9 | for row in range(h): 10 | for col in range(w): 11 | pv = gray[row, col] 12 | hist[pv] += 1 13 | 14 | y_pos = np.arange(0, 256, 1, dtype=np.int32) 15 | plt.bar(y_pos, hist, align='center', color='r', alpha=0.5) 16 | plt.xticks(y_pos, y_pos) 17 | plt.ylabel('Frequency') 18 | plt.title('Histogram') 19 | plt.show() 20 | 21 | 22 | src = cv.imread("./test.png") 23 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 24 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 25 | cv.imshow("input", gray) 26 | dst = cv.equalizeHist(gray) 27 | cv.imshow("eh", dst) 28 | 29 | custom_hist(gray) 30 | custom_hist(dst) 31 | 32 | cv.waitKey(0) 33 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_018/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_018/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_019/Mat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_019/Mat.png -------------------------------------------------------------------------------- /OpenCV_learn/code_019/opencv_019.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src1 = imread("./test.png"); 9 | Mat src2 = imread("./Mat.png"); 10 | Mat src3 = imread("./test.png"); 11 | Mat src4 = imread("./test.png"); 12 | 13 | imshow("input1", src1); 14 | imshow("input2", src2); 15 | imshow("input3", src3); 16 | imshow("input4", src4); 17 | 18 | Mat hsv1, hsv2, hsv3, hsv4; 19 | cvtColor(src1, hsv1, COLOR_BGR2HSV); 20 | cvtColor(src2, hsv2, COLOR_BGR2HSV); 21 | cvtColor(src3, hsv3, COLOR_BGR2HSV); 22 | cvtColor(src4, hsv4, COLOR_BGR2HSV); 23 | 24 | int h_bins = 60; int s_bins = 64; 25 | int histSize[] = { h_bins, s_bins }; 26 | float h_ranges[] = { 0, 180 }; 27 | float s_ranges[] = { 0, 256 }; 28 | const float* ranges[] = { h_ranges, s_ranges }; 29 | int channels[] = { 0, 1 }; 30 | Mat hist1, hist2, hist3, hist4; 31 | calcHist(&hsv1, 1, channels, Mat(), hist1, 2, histSize, ranges, true, false); 32 | calcHist(&hsv2, 1, channels, Mat(), hist2, 2, histSize, ranges, true, false); 33 | calcHist(&hsv3, 1, channels, Mat(), hist3, 2, histSize, ranges, true, false); 34 | calcHist(&hsv4, 1, channels, Mat(), hist4, 2, histSize, ranges, true, false); 35 | 36 | normalize(hist1, hist1, 0, 1, NORM_MINMAX, -1, Mat()); 37 | normalize(hist2, hist2, 0, 1, NORM_MINMAX, -1, Mat()); 38 | normalize(hist3, hist3, 0, 1, NORM_MINMAX, -1, Mat()); 39 | normalize(hist4, hist4, 0, 1, NORM_MINMAX, -1, Mat()); 40 | 41 | for (int i = 0; i < 4; i++) 42 | { 43 | int compare_method = i; 44 | double src1_src2 = compareHist(hist1, hist2, compare_method); 45 | double src3_src4 = compareHist(hist3, hist4, compare_method); 46 | printf(" Method [%d] : src1_src2 : %f, src3_src4: %f, \n", i, src1_src2, src3_src4); 47 | } 48 | 49 | waitKey(0); 50 | return 0; 51 | } 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /OpenCV_learn/code_019/opencv_019.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src1 = cv.imread("./test.png") 5 | src2 = cv.imread("./Mat.png") 6 | src3 = cv.imread("./test.png") 7 | src4 = cv.imread("./test.png") 8 | 9 | cv.imshow("input1", src1) 10 | cv.imshow("input2", src2) 11 | cv.imshow("input3", src3) 12 | cv.imshow("input4", src4) 13 | 14 | hsv1 = cv.cvtColor(src1, cv.COLOR_BGR2HSV) 15 | hsv2 = cv.cvtColor(src2, cv.COLOR_BGR2HSV) 16 | hsv3 = cv.cvtColor(src3, cv.COLOR_BGR2HSV) 17 | hsv4 = cv.cvtColor(src4, cv.COLOR_BGR2HSV) 18 | 19 | hist1 = cv.calcHist([hsv1], [0, 1], None, [60, 64], [0, 180, 0, 256]) 20 | hist2 = cv.calcHist([hsv2], [0, 1], None, [60, 64], [0, 180, 0, 256]) 21 | hist3 = cv.calcHist([hsv3], [0, 1], None, [60, 64], [0, 180, 0, 256]) 22 | hist4 = cv.calcHist([hsv4], [0, 1], None, [60, 64], [0, 180, 0, 256]) 23 | 24 | cv.normalize(hist1, hist1, 0, 1.0, cv.NORM_MINMAX) 25 | cv.normalize(hist2, hist2, 0, 1.0, cv.NORM_MINMAX) 26 | cv.normalize(hist3, hist3, 0, 1.0, cv.NORM_MINMAX) 27 | cv.normalize(hist4, hist4, 0, 1.0, cv.NORM_MINMAX) 28 | 29 | methods = [cv.HISTCMP_CORREL, cv.HISTCMP_CHISQR, 30 | cv.HISTCMP_INTERSECT, cv.HISTCMP_BHATTACHARYYA] 31 | str_method = "" 32 | for method in methods: 33 | src1_src2 = cv.compareHist(hist1, hist2, method) 34 | src3_src4 = cv.compareHist(hist3, hist4, method) 35 | if method == cv.HISTCMP_CORREL: 36 | str_method = "Correlation" 37 | if method == cv.HISTCMP_CHISQR: 38 | str_method = "Chi-square" 39 | if method == cv.HISTCMP_INTERSECT: 40 | str_method = "Intersection" 41 | if method == cv.HISTCMP_BHATTACHARYYA: 42 | str_method = "Bhattacharyya" 43 | 44 | print("%s src1_src2 = %.2f, src3_src4 = %.2f"%(str_method, src1_src2, src3_src4)) 45 | 46 | 47 | cv.waitKey(0) 48 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_019/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_019/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_020/Mat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_020/Mat.png -------------------------------------------------------------------------------- /OpenCV_learn/code_020/opencv_020.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | const int bins = 256; 8 | Mat src; 9 | const char* winTitle = "input image"; 10 | void backProjection_demo(Mat &image, Mat &model); 11 | int main(int argc, char** argv) { 12 | Mat src = imread("./Mat.png"); 13 | Mat model = imread("./test.png"); 14 | if (src.empty() || model.empty()) { 15 | printf("could not load image...\n"); 16 | return 0; 17 | } 18 | namedWindow(winTitle, WINDOW_AUTOSIZE); 19 | imshow(winTitle, src); 20 | imshow("model", model); 21 | 22 | backProjection_demo(src, model); 23 | waitKey(0); 24 | return 0; 25 | } 26 | 27 | void backProjection_demo(Mat &image, Mat &model) { 28 | Mat model_hsv, image_hsv; 29 | cvtColor(model, model_hsv, COLOR_BGR2HSV); 30 | cvtColor(image, image_hsv, COLOR_BGR2HSV); 31 | 32 | // ����ֱ��ͼ���������� 33 | int h_bins = 32; int s_bins = 32; 34 | int histSize[] = { h_bins, s_bins }; 35 | // hue varies from 0 to 179, saturation from 0 to 255 36 | float h_ranges[] = { 0, 180 }; 37 | float s_ranges[] = { 0, 256 }; 38 | const float* ranges[] = { h_ranges, s_ranges }; 39 | int channels[] = { 0, 1 }; 40 | Mat roiHist; 41 | calcHist(&model_hsv, 1, channels, Mat(), roiHist, 2, histSize, ranges, true, false); 42 | normalize(roiHist, roiHist, 0, 255, NORM_MINMAX, -1, Mat()); 43 | MatND backproj; 44 | calcBackProject(&image_hsv, 1, channels, roiHist, backproj, ranges, 1.0); 45 | imshow("BackProj", backproj); 46 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_020/opencv_020.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def back_projection_demo(): 7 | sample = cv.imread("./Mat.png") 8 | # hist2d_demo(sample) 9 | target = cv.imread("./test.png") 10 | # hist2d_demo(target) 11 | roi_hsv = cv.cvtColor(sample, cv.COLOR_BGR2HSV) 12 | target_hsv = cv.cvtColor(target, cv.COLOR_BGR2HSV) 13 | 14 | # show images 15 | cv.imshow("sample", sample) 16 | cv.imshow("target", target) 17 | 18 | roiHist = cv.calcHist([roi_hsv], [0, 1], None, [32, 32], [0, 180, 0, 256]) 19 | cv.normalize(roiHist, roiHist, 0, 255, cv.NORM_MINMAX) 20 | dst = cv.calcBackProject([target_hsv], [0, 1], roiHist, [0, 180, 0, 256], 1) 21 | cv.imshow("backProjectionDemo", dst) 22 | 23 | 24 | def hist2d_demo(image): 25 | hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV) 26 | hist = cv.calcHist([hsv], [0, 1], None, [32, 32], [0, 180, 0, 256]) 27 | dst = cv.resize(hist, (400, 400)) 28 | cv.imshow("image", image) 29 | cv.imshow("hist", dst) 30 | plt.imshow(hist, interpolation='nearest') 31 | plt.title("2D Histogram") 32 | plt.show() 33 | 34 | 35 | back_projection_demo() 36 | cv.waitKey(0) 37 | 38 | cv.destroyAllWindows() 39 | -------------------------------------------------------------------------------- /OpenCV_learn/code_020/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_020/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_021/opencv_021.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | int h = src.rows; 17 | int w = src.cols; 18 | 19 | // 3x3 ��ֵģ�����Զ���汾ʵ�� 20 | Mat dst = src.clone(); 21 | for (int row = 1; row < h-1; row++) { 22 | for (int col = 1; col < w-1; col++) { 23 | Vec3b p1 = src.at(row-1, col-1); 24 | Vec3b p2 = src.at(row-1, col); 25 | Vec3b p3 = src.at(row-1, col+1); 26 | Vec3b p4 = src.at(row, col-1); 27 | Vec3b p5 = src.at(row, col); 28 | Vec3b p6 = src.at(row, col+1); 29 | Vec3b p7 = src.at(row+1, col-1); 30 | Vec3b p8 = src.at(row+1, col); 31 | Vec3b p9 = src.at(row+1, col+1); 32 | 33 | int b = p1[0] + p2[0] + p3[0] + p4[0] + p5[0] + p6[0] + p7[0] + p8[0] + p9[0]; 34 | int g = p1[1] + p2[1] + p3[1] + p4[1] + p5[1] + p6[1] + p7[1] + p8[1] + p9[1]; 35 | int r = p1[2] + p2[2] + p3[2] + p4[2] + p5[2] + p6[2] + p7[2] + p8[2] + p9[2]; 36 | 37 | dst.at(row, col)[0] = saturate_cast(b / 9); 38 | dst.at(row, col)[1] = saturate_cast(g / 9); 39 | dst.at(row, col)[2] = saturate_cast(r / 9); 40 | } 41 | } 42 | imshow("blur", dst); 43 | imwrite("D:/result.png", dst); 44 | 45 | // OpenCV ��ֵģ�� 46 | Mat result; 47 | blur(src, result, Size(15, 15), Point(-1, -1), 4); 48 | imshow("result", result); 49 | 50 | waitKey(0); 51 | return 0; 52 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_021/opencv_021.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def custom_blur(src): 6 | h, w, ch = src.shape 7 | print("h , w, ch", h, w, ch) 8 | result = np.copy(src) 9 | for row in range(1, h-1, 1): 10 | for col in range(1, w-1, 1): 11 | v1 = np.int32(src[row-1, col-1]) 12 | v2 = np.int32(src[row-1, col]) 13 | v3 = np.int32(src[row-1, col+1]) 14 | v4 = np.int32(src[row, col-1]) 15 | v5 = np.int32(src[row, col]) 16 | v6 = np.int32(src[row, col+1]) 17 | v7 = np.int32(src[row+1, col-1]) 18 | v8 = np.int32(src[row+1, col]) 19 | v9 = np.int32(src[row+1, col+1]) 20 | 21 | b = v1[0] + v2[0] + v3[0] + v4[0] + v5[0] + v6[0] + v7[0] + v8[0] + v9[0]; 22 | g = v1[1] + v2[1] + v3[1] + v4[1] + v5[1] + v6[1] + v7[1] + v8[1] + v9[1]; 23 | r = v1[2] + v2[2] + v3[2] + v4[2] + v5[2] + v6[2] + v7[2] + v8[2] + v9[2]; 24 | result[row, col] = [b//9, g//9, r//9] 25 | cv.imshow("result", result) 26 | 27 | 28 | src = cv.imread("./test.png") 29 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 30 | cv.imshow("input", src) 31 | dst = cv.blur(src, (15, 15)) 32 | cv.imshow("blur", dst) 33 | custom_blur(src) 34 | cv.waitKey(0) 35 | cv.destroyAllWindows() 36 | 37 | -------------------------------------------------------------------------------- /OpenCV_learn/code_021/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_021/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_022/opencv_022.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat dst1, dst2; 17 | blur(src, dst1, Size(5, 5), Point(-1, -1), 4); 18 | GaussianBlur(src, dst2, Size(5, 5), 15, 0, 4); 19 | 20 | 21 | imshow("blur", dst1); 22 | imshow("gaussian blur", dst2); 23 | 24 | waitKey(0); 25 | return 0; 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /OpenCV_learn/code_022/opencv_022.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | src = cv.imread("./test.png") 6 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 7 | cv.imshow("input", src) 8 | 9 | dst1 = cv.blur(src, (5, 5)) 10 | dst2 = cv.GaussianBlur(src, (5, 5), sigmaX=15) 11 | dst3 = cv.GaussianBlur(src, (0, 0), sigmaX=15) 12 | 13 | cv.imshow("blur ksize=5", dst1) 14 | cv.imshow("gaussian ksize=5", dst2) 15 | cv.imshow("gaussian sigmax=15", dst3) 16 | 17 | cv.waitKey(0) 18 | cv.destroyAllWindows() 19 | 20 | 21 | -------------------------------------------------------------------------------- /OpenCV_learn/code_022/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_022/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_023/opencv_23.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat dst; 17 | medianBlur(src, dst, 5); 18 | imshow("medianblur ksize=5", dst); 19 | 20 | waitKey(0); 21 | return 0; 22 | } 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /OpenCV_learn/code_023/opencv_23.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | src = cv.imread("./test.png") 6 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 7 | cv.imshow("input", src) 8 | 9 | dst = cv.medianBlur(src, 5) 10 | cv.imshow("blur ksize=5", dst) 11 | 12 | cv.waitKey(0) 13 | cv.destroyAllWindows() 14 | 15 | 16 | -------------------------------------------------------------------------------- /OpenCV_learn/code_024/opencv_024.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | void add_salt_pepper_noise(Mat &image); 8 | void gaussian_noise(Mat &image); 9 | int main(int artc, char** argv) { 10 | Mat src = imread("./test.png"); 11 | if (src.empty()) { 12 | printf("could not load image...\n"); 13 | return -1; 14 | } 15 | namedWindow("input", CV_WINDOW_AUTOSIZE); 16 | imshow("input", src); 17 | gaussian_noise(src); 18 | 19 | waitKey(0); 20 | return 0; 21 | } 22 | 23 | void add_salt_pepper_noise(Mat &image) { 24 | RNG rng(12345); 25 | int h = image.rows; 26 | int w = image.cols; 27 | int nums = 10000; 28 | for (int i = 0; i < nums; i++) { 29 | int x = rng.uniform(0, w); 30 | int y = rng.uniform(0, h); 31 | if (i % 2 == 1) { 32 | image.at(y, x) = Vec3b(255, 255, 255); 33 | } 34 | else { 35 | image.at(y, x) = Vec3b(0, 0, 0); 36 | } 37 | } 38 | imshow("salt pepper", image); 39 | } 40 | 41 | void gaussian_noise(Mat &image) { 42 | Mat noise = Mat::zeros(image.size(), image.type()); 43 | randn(noise, (15, 15, 15), (30, 30, 30)); 44 | Mat dst; 45 | add(image, noise, dst); 46 | imshow("gaussian noise", dst); 47 | } 48 | -------------------------------------------------------------------------------- /OpenCV_learn/code_024/opencv_024.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def add_salt_pepper_noise(image): 6 | h, w = image.shape[:2] 7 | nums = 10000 8 | rows = np.random.randint(0, h, nums, dtype=np.int) 9 | cols = np.random.randint(0, w, nums, dtype=np.int) 10 | for i in range(nums): 11 | if i % 2 == 1: 12 | image[rows[i], cols[i]] = (255, 255, 255) 13 | else: 14 | image[rows[i], cols[i]] = (0, 0, 0) 15 | return image 16 | 17 | 18 | def gaussian_noise(image): 19 | noise = np.zeros(image.shape, image.dtype) 20 | m = (15, 15, 15) 21 | s = (30, 30, 30) 22 | cv.randn(noise, m, s) 23 | dst = cv.add(image, noise) 24 | cv.imshow("gaussian noise", dst) 25 | return dst 26 | 27 | 28 | src = cv.imread("./test.png") 29 | h, w = src.shape[:2] 30 | copy = np.copy(src) 31 | copy = add_salt_pepper_noise(copy) 32 | 33 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 34 | result[0:h,0:w,:] = src 35 | result[0:h,w:2*w,:] = copy 36 | cv.putText(result, "original image", (10, 30), cv.FONT_HERSHEY_PLAIN, 2.0, (0, 255, 255), 1) 37 | cv.putText(result, "salt pepper image", (w+10, 30), cv.FONT_HERSHEY_PLAIN, 2.0, (0, 255, 255), 1) 38 | cv.imshow("salt pepper noise", result) 39 | cv.imwrite("./result.png", result) 40 | 41 | cv.waitKey(0) 42 | cv.destroyAllWindows() 43 | 44 | 45 | -------------------------------------------------------------------------------- /OpenCV_learn/code_024/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_024/result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_024/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_024/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_025/opencv_025.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | void add_salt_pepper_noise(Mat &image); 8 | void gaussian_noise(Mat &image); 9 | int main(int artc, char** argv) { 10 | Mat src = imread("./test.png"); 11 | if (src.empty()) { 12 | printf("could not load image...\n"); 13 | return -1; 14 | } 15 | namedWindow("input", CV_WINDOW_AUTOSIZE); 16 | imshow("input", src); 17 | gaussian_noise(src); 18 | 19 | Mat result1, result2, result3, result4; 20 | blur(src, result1, Size(5, 5)); 21 | imshow("result-1", result1); 22 | 23 | GaussianBlur(src, result2, Size(5, 5), 0); 24 | imshow("result-2", result2); 25 | 26 | medianBlur(src, result3, 5); 27 | imshow("result-3", result3); 28 | 29 | fastNlMeansDenoisingColored(src, result4, 15, 15, 10, 30); 30 | imshow("result-4", result4); 31 | 32 | waitKey(0); 33 | return 0; 34 | } 35 | 36 | void add_salt_pepper_noise(Mat &image) { 37 | RNG rng(12345); 38 | int h = image.rows; 39 | int w = image.cols; 40 | int nums = 10000; 41 | for (int i = 0; i < nums; i++) { 42 | int x = rng.uniform(0, w); 43 | int y = rng.uniform(0, h); 44 | if (i % 2 == 1) { 45 | image.at(y, x) = Vec3b(255, 255, 255); 46 | } 47 | else { 48 | image.at(y, x) = Vec3b(0, 0, 0); 49 | } 50 | } 51 | imshow("salt pepper", image); 52 | } 53 | 54 | void gaussian_noise(Mat &image) { 55 | Mat noise = Mat::zeros(image.size(), image.type()); 56 | randn(noise, (15, 15, 15), (30, 30, 30)); 57 | Mat dst; 58 | add(image, noise, dst); 59 | imshow("gaussian noise", dst); 60 | dst.copyTo(image); 61 | } 62 | -------------------------------------------------------------------------------- /OpenCV_learn/code_025/opencv_025.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import cv2 as cv 3 | import numpy as np 4 | 5 | 6 | def add_salt_pepper_noise(image): 7 | h, w = image.shape[:2] 8 | nums = 10000 9 | rows = np.random.randint(0, h, nums, dtype=np.int) 10 | cols = np.random.randint(0, w, nums, dtype=np.int) 11 | for i in range(nums): 12 | if i % 2 == 1: 13 | image[rows[i], cols[i]] = (255, 255, 255) 14 | else: 15 | image[rows[i], cols[i]] = (0, 0, 0) 16 | return image 17 | 18 | 19 | def gaussian_noise(image): 20 | noise = np.zeros(image.shape, image.dtype) 21 | m = (15, 15, 15) 22 | s = (30, 30, 30) 23 | cv.randn(noise, m, s) 24 | dst = cv.add(image, noise) 25 | cv.imshow("gaussian noise", dst) 26 | return dst 27 | 28 | 29 | src = cv.imread("./test.png") 30 | cv.imshow("input", src) 31 | h, w = src.shape[:2] 32 | src = gaussian_noise(src) 33 | 34 | result1 = cv.blur(src, (5, 5)) 35 | cv.imshow("result-1", result1) 36 | 37 | result2 = cv.GaussianBlur(src, (5, 5), 0) 38 | cv.imshow("result-2", result2) 39 | 40 | result3 = cv.medianBlur(src, 5) 41 | cv.imshow("result-3", result3) 42 | 43 | result4 = cv.fastNlMeansDenoisingColored(src, None, 15, 15, 10, 30) 44 | cv.imshow("result-4", result4) 45 | 46 | cv.waitKey(0) 47 | cv.destroyAllWindows() 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /OpenCV_learn/code_025/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_025/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_026/opencv_026.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | h, w = src.shape[:2] 9 | dst = cv.bilateralFilter(src, 0, 100, 10) 10 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 11 | result[0:h,0:w,:] = src 12 | result[0:h,w:2*w,:] = dst 13 | result = cv.resize(result, (w, h//2)) 14 | cv.imshow("result", result) 15 | 16 | cv.waitKey(0) 17 | cv.destroyAllWindows() 18 | -------------------------------------------------------------------------------- /OpenCV_learn/code_026/openv_026.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat dst; 17 | bilateralFilter(src, dst, 0, 100, 10, 4); 18 | imshow("result", dst); 19 | 20 | waitKey(0); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /OpenCV_learn/code_026/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_026/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_027/opencv_027.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat dst; 17 | pyrMeanShiftFiltering(src, dst, 15, 50, 1, TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 5, 1)); 18 | imshow("result", dst); 19 | 20 | waitKey(0); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /OpenCV_learn/code_027/opencv_027.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | h, w = src.shape[:2] 9 | dst = cv.pyrMeanShiftFiltering(src, 15, 30, termcrit=(cv.TERM_CRITERIA_MAX_ITER+cv.TERM_CRITERIA_EPS, 5, 1)) 10 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 11 | result[0:h,0:w,:] = src 12 | result[0:h,w:2*w,:] = dst 13 | result = cv.resize(result,(w, h//2)) 14 | cv.imshow("result", result) 15 | # cv.imwrite("result.png", result) 16 | 17 | cv.waitKey(0) 18 | cv.destroyAllWindows() 19 | -------------------------------------------------------------------------------- /OpenCV_learn/code_027/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_027/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_028/opencv_028.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def get_block_sum(ii, x1, y1, x2, y2, index): 6 | tl = ii[y1, x1][index] 7 | tr = ii[y2, x1][index] 8 | bl = ii[y1, x2][index] 9 | br = ii[y2, x2][index] 10 | s = (br - bl - tr + tl) 11 | return s 12 | 13 | 14 | def blur_demo(image, ii): 15 | h, w, dims = image.shape 16 | result = np.zeros(image.shape, image.dtype) 17 | ksize = 15 18 | radius = ksize // 2 19 | for row in range(0, h + radius, 1): 20 | y2 = h if (row + 1)> h else (row + 1) 21 | y1 = 0 if (row - ksize) < 0 else (row - ksize) 22 | for col in range(0, w + radius, 1): 23 | x2 = w if (col + 1)>w else (col + 1) 24 | x1 = 0 if (col - ksize) < 0 else (col - ksize) 25 | cx = 0 if (col - radius) < 0 else (col - radius) 26 | cy = 0 if (row - radius) < 0 else (row - radius) 27 | num = (x2 - x1)*(y2 - y1) 28 | for i in range(0, 3, 1): 29 | s = get_block_sum(ii, x1, y1, x2, y2, i) 30 | result[cy, cx][i] = s // num 31 | 32 | cv.imshow("integral fast blur", result) 33 | # cv.imwrite("./result.png", result) 34 | 35 | 36 | src = cv.imread("./test.png") 37 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 38 | cv.imshow("input", src) 39 | sum_table = cv.integral(src, sdepth=cv.CV_32S) 40 | blur_demo(src, sum_table) 41 | 42 | cv.waitKey(0) 43 | cv.destroyAllWindows() 44 | -------------------------------------------------------------------------------- /OpenCV_learn/code_028/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_028/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_029/opencv_029.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat dst; 17 | double tt = getTickCount(); 18 | edgePreservingFilter(src, dst, 1, 60, 0.44); 19 | double end = (getTickCount() - tt) / getTickFrequency(); 20 | printf("time consume : %f\n ", end); 21 | imshow("result", dst); 22 | 23 | waitKey(0); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /OpenCV_learn/code_029/opencv_029.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | h, w = src.shape[:2] 9 | dst = cv.edgePreservingFilter(src, sigma_s=100, sigma_r=0.4, flags=cv.RECURS_FILTER) 10 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 11 | result[0:h,0:w,:] = src 12 | result[0:h,w:2*w,:] = dst 13 | result = cv.resize(result, (w, h//2) ) 14 | cv.imshow("result", result) 15 | # cv.imwrite("result.png", result) 16 | 17 | 18 | cv.waitKey(0) 19 | cv.destroyAllWindows() 20 | -------------------------------------------------------------------------------- /OpenCV_learn/code_029/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_029/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_030/opencv_030.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat kernel1 = Mat::ones(5, 5, CV_32F) / (float)(25); 17 | 18 | Mat kernel2 = (Mat_(3, 3) << 0, -1, 0, 19 | -1, 5, -1, 20 | 0, -1, 0); 21 | 22 | Mat kernel3 = (Mat_(2, 2) << 1, 0, 0, -1); 23 | 24 | Mat dst1, dst2, dst3; 25 | filter2D(src, dst1, -1, kernel1); 26 | filter2D(src, dst2, -1, kernel2); 27 | filter2D(src, dst3, CV_32F, kernel3); 28 | convertScaleAbs(dst3, dst3); 29 | 30 | imshow("blur=5x5", dst1); 31 | imshow("shape=3x3", dst2); 32 | imshow("gradient=2x2", dst3); 33 | 34 | waitKey(0); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /OpenCV_learn/code_030/opencv_030.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | blur_op = np.ones([5, 5], dtype=np.float32)/25. 9 | shape_op = np.array([[0, -1, 0], 10 | [-1, 5, -1], 11 | [0, -1, 0]], np.float32) 12 | grad_op = np.array([[1, 0],[0, -1]], dtype=np.float32) 13 | 14 | dst1 = cv.filter2D(src, -1, blur_op) 15 | dst2 = cv.filter2D(src, -1, shape_op) 16 | dst3 = cv.filter2D(src, cv.CV_32F, grad_op) 17 | dst3 = cv.convertScaleAbs(dst3) 18 | 19 | cv.imshow("blur=5x5", dst1); 20 | cv.imshow("shape=3x3", dst2); 21 | cv.imshow("gradient=2x2", dst3); 22 | 23 | cv.waitKey(0) 24 | cv.destroyAllWindows() 25 | 26 | -------------------------------------------------------------------------------- /OpenCV_learn/code_030/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_030/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_031/opencv_031.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat grad_x, grad_y; 17 | Mat dst; 18 | Sobel(src, grad_x, CV_32F, 1, 0, 3, 1, 0, BORDER_DEFAULT); 19 | Sobel(src, grad_y, CV_32F, 0, 1, 3, 1, 0, BORDER_DEFAULT); 20 | 21 | convertScaleAbs(grad_x, grad_x); 22 | convertScaleAbs(grad_y, grad_y); 23 | 24 | add(grad_x, grad_y, dst, Mat(), CV_16S); 25 | convertScaleAbs(dst, dst); 26 | 27 | imshow("gradient", dst); 28 | 29 | waitKey(0); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /OpenCV_learn/code_031/opencv_031.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | h, w = src.shape[:2] 9 | x_grad = cv.Sobel(src, cv.CV_32F, 1, 0) 10 | y_grad = cv.Sobel(src, cv.CV_32F, 0, 1) 11 | 12 | x_grad = cv.convertScaleAbs(x_grad) 13 | y_grad = cv.convertScaleAbs(y_grad) 14 | # cv.imshow("x_grad", x_grad) 15 | # cv.imshow("y_grad", y_grad) 16 | 17 | dst = cv.add(x_grad, y_grad, dtype=cv.CV_16S) 18 | dst = cv.convertScaleAbs(dst) 19 | cv.imshow("gradient", dst) 20 | 21 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 22 | result[0:h,0:w,:] = src 23 | result[0:h,w:2*w,:] = dst 24 | cv.imshow("result", result) 25 | cv.imwrite("./result.png", dst) 26 | 27 | cv.waitKey(0) 28 | cv.destroyAllWindows() 29 | -------------------------------------------------------------------------------- /OpenCV_learn/code_031/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_031/result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_031/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_031/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_032/opencv_032.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat robert_x = (Mat_(2, 2) << 1, 0, 0, -1); 17 | Mat robert_y = (Mat_(2, 2) << 0, -1, 1, 0); 18 | 19 | Mat prewitt_x = (Mat_(3, 3) << -1, 0, 1, 20 | -1, 0, 1, 21 | -1, 0, 1); 22 | Mat prewitt_y = (Mat_(3, 3) << -1, -1, -1, 23 | 0, 0, 0, 24 | 1, 1, 1); 25 | 26 | Mat robert_grad_x, robert_grad_y, prewitt_grad_x, prewitt_grad_y; 27 | filter2D(src, robert_grad_x, CV_16S, robert_x); 28 | filter2D(src, robert_grad_y, CV_16S, robert_y); 29 | convertScaleAbs(robert_grad_x, robert_grad_x); 30 | convertScaleAbs(robert_grad_y, robert_grad_y); 31 | 32 | filter2D(src, prewitt_grad_x, CV_32F, prewitt_x); 33 | filter2D(src, prewitt_grad_y, CV_32F, prewitt_y); 34 | convertScaleAbs(prewitt_grad_x, prewitt_grad_x); 35 | convertScaleAbs(prewitt_grad_y, prewitt_grad_y); 36 | printf("image gradient..."); 37 | 38 | imshow("robert x", robert_grad_x); 39 | imshow("robert y", robert_grad_y); 40 | imshow("prewitt x", prewitt_grad_x); 41 | imshow("prewitt y", prewitt_grad_y); 42 | 43 | waitKey(0); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /OpenCV_learn/code_032/opencv_032.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | robert_x = np.array([[1, 0],[0, -1]], dtype=np.float32) 9 | robert_y = np.array([[0, -1],[1, 0]], dtype=np.float32) 10 | 11 | prewitt_x = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=np.float32) 12 | prewitt_y = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]], dtype=np.float32) 13 | 14 | robert_grad_x = cv.filter2D(src, cv.CV_16S, robert_x) 15 | robert_grad_y = cv.filter2D(src, cv.CV_16S, robert_y) 16 | robert_grad_x = cv.convertScaleAbs(robert_grad_x) 17 | robert_grad_y = cv.convertScaleAbs(robert_grad_y) 18 | 19 | prewitt_grad_x = cv.filter2D(src, cv.CV_32F, prewitt_x) 20 | prewitt_grad_y = cv.filter2D(src, cv.CV_32F, prewitt_y) 21 | prewitt_grad_x = cv.convertScaleAbs(prewitt_grad_x) 22 | prewitt_grad_y = cv.convertScaleAbs(prewitt_grad_y) 23 | 24 | # cv.imshow("robert x", robert_grad_x); 25 | # cv.imshow("robert y", robert_grad_y); 26 | # cv.imshow("prewitt x", prewitt_grad_x); 27 | # cv.imshow("prewitt y", prewitt_grad_y); 28 | 29 | h, w = src.shape[:2] 30 | robert_result = np.zeros([h, w*2, 3], dtype=src.dtype) 31 | robert_result[0:h,0:w,:] = robert_grad_x 32 | robert_result[0:h,w:2*w,:] = robert_grad_y 33 | cv.imshow("robert_result", robert_result) 34 | 35 | prewitt_result = np.zeros([h, w*2, 3], dtype=src.dtype) 36 | prewitt_result[0:h,0:w,:] = prewitt_grad_x 37 | prewitt_result[0:h,w:2*w,:] = prewitt_grad_y 38 | cv.imshow("prewitt_result", prewitt_result) 39 | 40 | cv.imwrite("./prewitt.png", prewitt_result) 41 | cv.imwrite("./robert.png", robert_result) 42 | 43 | cv.waitKey(0) 44 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_032/prewitt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_032/prewitt.png -------------------------------------------------------------------------------- /OpenCV_learn/code_032/robert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_032/robert.png -------------------------------------------------------------------------------- /OpenCV_learn/code_032/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_032/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_033/aplacian.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_033/aplacian.png -------------------------------------------------------------------------------- /OpenCV_learn/code_033/opencv_033.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat image = imread("./test.png"); 9 | if (image.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", image); 15 | 16 | Mat blured, dst; 17 | GaussianBlur(image, blured, Size(3, 3), 0); 18 | Laplacian(blured, dst, CV_32F, 1, 1.0, 127.0, BORDER_DEFAULT); 19 | convertScaleAbs(dst, dst); 20 | imshow("result", dst); 21 | 22 | waitKey(0); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /OpenCV_learn/code_033/opencv_033.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | image = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", image) 7 | 8 | h, w = image.shape[:2] 9 | src = cv.GaussianBlur(image, (0, 0), 1) 10 | dst = cv.Laplacian(src, cv.CV_32F, ksize=3, delta=127) 11 | dst = cv.convertScaleAbs(dst) 12 | result = np.zeros([h, w*2, 3], dtype=image.dtype) 13 | result[0:h,0:w,:] = image 14 | result[0:h,w:2*w,:] = dst 15 | cv.imshow("result", result) 16 | cv.imwrite("./aplacian.png", result) 17 | 18 | 19 | cv.waitKey(0) 20 | cv.destroyAllWindows() 21 | -------------------------------------------------------------------------------- /OpenCV_learn/code_033/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_033/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_034/opencv_034.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat sharpen_op = (Mat_(3, 3) << 0, -1, 0, 17 | -1, 5, -1, 18 | 0, -1, 0); 19 | 20 | Mat result; 21 | filter2D(src, result, CV_32F, sharpen_op); 22 | convertScaleAbs(result, result); 23 | 24 | imshow("sharpen image", result); 25 | 26 | waitKey(0); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /OpenCV_learn/code_034/opencv_034.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # sharpen_op = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]], dtype=np.float32) 9 | sharpen_op = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], dtype=np.float32) 10 | sharpen_image = cv.filter2D(src, cv.CV_32F, sharpen_op) 11 | sharpen_image = cv.convertScaleAbs(sharpen_image) 12 | cv.imshow("sharpen_image", sharpen_image) 13 | 14 | h, w = src.shape[:2] 15 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 16 | result[0:h,0:w,:] = src 17 | result[0:h,w:2*w,:] = sharpen_image 18 | cv.putText(result, "original image", (10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 19 | cv.putText(result, "sharpen image", (w+10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 20 | cv.imshow("sharpen_image", result) 21 | cv.imwrite("./result.png", result) 22 | 23 | cv.waitKey(0) 24 | cv.destroyAllWindows() 25 | -------------------------------------------------------------------------------- /OpenCV_learn/code_034/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_034/result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_034/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_034/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_035/opencv_035.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat blur_img, usm; 17 | GaussianBlur(src, blur_img, Size(0, 0), 25); 18 | addWeighted(src, 1.5, blur_img, -0.5, 0, usm); 19 | imshow("mask image", usm); 20 | 21 | waitKey(0); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /OpenCV_learn/code_035/opencv_035.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # sigma = 5、15、25 9 | blur_img = cv.GaussianBlur(src, (0, 0), 5) 10 | usm = cv.addWeighted(src, 1.5, blur_img, -0.5, 0) 11 | cv.imshow("mask image", usm) 12 | 13 | h, w = src.shape[:2] 14 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 15 | result[0:h,0:w,:] = src 16 | result[0:h,w:2*w,:] = usm 17 | cv.putText(result, "original image", (10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 18 | cv.putText(result, "sharpen image", (w+10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 19 | cv.imshow("sharpen_image", result) 20 | cv.imwrite("./result.png", result) 21 | 22 | cv.waitKey(0) 23 | cv.destroyAllWindows() 24 | -------------------------------------------------------------------------------- /OpenCV_learn/code_035/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_035/result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_035/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_035/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_036/edge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_036/edge.png -------------------------------------------------------------------------------- /OpenCV_learn/code_036/opencv_036.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | Mat edges; 17 | Canny(src, edges, 100, 300, 3, false); 18 | imshow("edge image", edges); 19 | 20 | waitKey(0); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /OpenCV_learn/code_036/opencv_036.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # t1 = 100, t2 = 3*t1 = 300 9 | edge = cv.Canny(src, 100, 300) 10 | cv.imshow("mask image", edge) 11 | cv.imwrite("./edge.png", edge) 12 | edge_src = cv.bitwise_and(src, src, mask=edge) 13 | 14 | h, w = src.shape[:2] 15 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 16 | result[0:h,0:w,:] = src 17 | result[0:h,w:2*w,:] = edge_src 18 | cv.putText(result, "original image", (10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 19 | cv.putText(result, "edge image", (w+10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 20 | cv.imshow("edge detector", result) 21 | cv.imwrite("./result.png", result) 22 | 23 | cv.waitKey(0) 24 | cv.destroyAllWindows() 25 | -------------------------------------------------------------------------------- /OpenCV_learn/code_036/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_036/result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_036/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_036/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_037/opencv_037.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | void pyramid_up(Mat &image, vector &pyramid_images, int level); 8 | void pyramid_down(vector &pyramid_images); 9 | int main(int artc, char** argv) { 10 | Mat src = imread("./test.png"); 11 | if (src.empty()) { 12 | printf("could not load image...\n"); 13 | return -1; 14 | } 15 | namedWindow("input", CV_WINDOW_AUTOSIZE); 16 | imshow("input", src); 17 | 18 | vector p_images; 19 | pyramid_up(src, p_images, 3); 20 | pyramid_down(p_images); 21 | 22 | waitKey(0); 23 | return 0; 24 | } 25 | 26 | void pyramid_up(Mat &image, vector &pyramid_images, int level) { 27 | Mat temp = image.clone(); 28 | Mat dst; 29 | for (int i = 0; i < level; i++) { 30 | pyrDown(temp, dst); 31 | //imshow(format("pyramid_up_%d", i), dst); 32 | temp = dst.clone(); 33 | pyramid_images.push_back(temp); 34 | } 35 | } 36 | 37 | void pyramid_down(vector &pyramid_images) { 38 | for (int t = pyramid_images.size() - 1; t>-1; t--) { 39 | Mat dst; 40 | pyrUp(pyramid_images[t], dst); 41 | imshow(format("pyramid_down_%d", t), dst); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /OpenCV_learn/code_037/opencv_037.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | 4 | def pyramid_down(pyramid_images): 5 | level = len(pyramid_images) 6 | print("level = ",level) 7 | for i in range(level-1, -1, -1): 8 | expand = cv.pyrUp(pyramid_images[i]) 9 | cv.imshow("pyramid_down_"+str(i), expand) 10 | 11 | 12 | def pyramid_up(image, level=3): 13 | temp = image.copy() 14 | # cv.imshow("input", image) 15 | pyramid_images = [] 16 | for i in range(level): 17 | dst = cv.pyrDown(temp) 18 | pyramid_images.append(dst) 19 | # cv.imshow("pyramid_up_" + str(i), dst) 20 | temp = dst.copy() 21 | return pyramid_images 22 | 23 | 24 | src = cv.imread("test.png") 25 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 26 | cv.imshow("input", src) 27 | # pyramid_up(src) 28 | pyramid_down(pyramid_up(src)) 29 | 30 | cv.waitKey(0) 31 | cv.destroyAllWindows() 32 | -------------------------------------------------------------------------------- /OpenCV_learn/code_037/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_037/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_038/opencv_038.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | void pyramid_up(Mat &image, vector &pyramid_images, int level); 8 | void laplaian_demo(vector &pyramid_images, Mat &image); 9 | int main(int artc, char** argv) { 10 | Mat src = imread("./test.png"); 11 | if (src.empty()) { 12 | printf("could not load image...\n"); 13 | return -1; 14 | } 15 | namedWindow("input", CV_WINDOW_AUTOSIZE); 16 | imshow("input", src); 17 | 18 | vector p_images; 19 | pyramid_up(src, p_images, 3); 20 | laplaian_demo(p_images, src); 21 | 22 | waitKey(0); 23 | return 0; 24 | } 25 | 26 | void pyramid_up(Mat &image, vector &pyramid_images, int level) { 27 | Mat temp = image.clone(); 28 | Mat dst; 29 | for (int i = 0; i < level; i++) { 30 | pyrDown(temp, dst); 31 | //imshow(format("pyramid_up_%d", i), dst); 32 | temp = dst.clone(); 33 | pyramid_images.push_back(temp); 34 | } 35 | } 36 | 37 | void laplaian_demo(vector &pyramid_images, Mat &image) { 38 | for (int t = pyramid_images.size() - 1; t>-1; t--) { 39 | Mat dst; 40 | if (t - 1 < 0) { 41 | pyrUp(pyramid_images[t], dst, image.size()); 42 | subtract(image, dst, dst); 43 | dst = dst + Scalar(127, 127, 127); 44 | imshow(format("laplaian_layer_%d", t), dst); 45 | } 46 | else { 47 | pyrUp(pyramid_images[t], dst, pyramid_images[t-1].size()); 48 | subtract(pyramid_images[t - 1], dst, dst); 49 | dst = dst + Scalar(127, 127, 127); 50 | imshow(format("laplaian_layer_%d", t), dst); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /OpenCV_learn/code_038/opencv_038.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def laplaian_demo(pyramid_images): 6 | level = len(pyramid_images) 7 | for i in range(level-1, -1, -1): 8 | if (i-1) < 0: 9 | h, w = src.shape[:2] 10 | expand = cv.pyrUp(pyramid_images[i], dstsize=(w, h)) 11 | lpls = cv.subtract(src, expand) + 127 12 | cv.imshow("lpls_" + str(i), lpls) 13 | else: 14 | h, w = pyramid_images[i-1].shape[:2] 15 | expand = cv.pyrUp(pyramid_images[i], dstsize=(w, h)) 16 | lpls = cv.subtract(pyramid_images[i-1], expand) + 127 17 | cv.imshow("lpls_"+str(i), lpls) 18 | 19 | 20 | def pyramid_up(image, level=3): 21 | temp = image.copy() 22 | # cv.imshow("input", image) 23 | pyramid_images = [] 24 | for i in range(level): 25 | dst = cv.pyrDown(temp) 26 | pyramid_images.append(dst) 27 | # cv.imshow("pyramid_up_" + str(i), dst) 28 | temp = dst.copy() 29 | return pyramid_images 30 | 31 | 32 | src = cv.imread("./test.png") 33 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 34 | cv.imshow("input", src) 35 | # pyramid_up(src) 36 | laplaian_demo(pyramid_up(src)) 37 | 38 | cv.waitKey(0) 39 | cv.destroyAllWindows() 40 | 41 | -------------------------------------------------------------------------------- /OpenCV_learn/code_038/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_038/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_039/opencv_039.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | const float t = 0.95; 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test01.png"); 9 | Mat tpl = imread("./test.png"); 10 | if (src.empty() || tpl.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | imshow("input", src); 15 | imshow("tpl", tpl); 16 | 17 | int result_h = src.rows - tpl.rows + 1; 18 | int result_w = src.cols - tpl.cols + 1; 19 | Mat result = Mat::zeros(Size(result_w, result_h), CV_32FC1); 20 | matchTemplate(src, tpl, result, TM_CCOEFF_NORMED); 21 | imshow("result image", result); 22 | int h = result.rows; 23 | int w = result.cols; 24 | for (int row = 0; row < h; row++) { 25 | for (int col = 0; col < w; col++) { 26 | float v = result.at(row, col); 27 | // printf("v = %.2f\n", v); 28 | if (v > t) { 29 | rectangle(src, Point(col, row), Point(col + tpl.cols, row + tpl.rows), Scalar(255, 0, 0), 1, 8, 0); 30 | } 31 | } 32 | } 33 | imshow("template matched result", src); 34 | 35 | waitKey(0); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /OpenCV_learn/code_039/opencv_039.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def template_demo(): 6 | src = cv.imread("./test.png") 7 | tpl = cv.imread("./test01.png") 8 | cv.imshow("input", src) 9 | cv.imshow("tpl", tpl) 10 | th, tw = tpl.shape[:2] 11 | result = cv.matchTemplate(src, tpl, cv.TM_CCORR_NORMED) 12 | cv.imshow("result", result) 13 | cv.imwrite("D:/039_003.png", np.uint8(result*255)) 14 | t = 0.98 15 | loc = np.where(result > t) 16 | 17 | for pt in zip(*loc[::-1]): 18 | cv.rectangle(src, pt, (pt[0] + tw, pt[1] + th), (255, 0, 0), 1, 8, 0) 19 | cv.imshow("llk-demo", src) 20 | cv.imwrite("D:/039_004.png", src) 21 | 22 | 23 | template_demo() 24 | cv.waitKey(0) 25 | cv.destroyAllWindows() 26 | -------------------------------------------------------------------------------- /OpenCV_learn/code_039/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_039/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_039/test01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_039/test01.png -------------------------------------------------------------------------------- /OpenCV_learn/code_040/opencv_040.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int artc, char** argv) { 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | namedWindow("input", CV_WINDOW_AUTOSIZE); 14 | imshow("input", src); 15 | 16 | int t = 127; 17 | Mat gray, binary; 18 | cvtColor(src, gray, COLOR_BGR2GRAY); 19 | Scalar m = mean(src); 20 | t = m[0]; 21 | binary = Mat::zeros(src.size(), CV_8UC1); 22 | 23 | // ֱ�Ӷ�ȡͼ������ 24 | int height = src.rows; 25 | int width = src.cols; 26 | for (int row = 0; row < height; row++) { 27 | for (int col = 0; col < width; col++) { 28 | int pv = gray.at(row, col); 29 | if (pv > t) { 30 | binary.at(row, col) = 255; 31 | } 32 | else { 33 | binary.at(row, col) = 0; 34 | } 35 | } 36 | } 37 | imshow("binary", binary); 38 | 39 | waitKey(0); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /OpenCV_learn/code_040/opencv_040.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | T = 127 9 | 10 | # 转换为灰度图像 11 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 12 | h, w = gray.shape 13 | T = cv.mean(gray)[0] 14 | print("current threshold value : ", T) 15 | 16 | # 二值图像 17 | binary = np.zeros((h, w), dtype=np.uint8) 18 | for row in range(h): 19 | for col in range(w): 20 | pv = gray[row, col] 21 | if pv > T: 22 | binary[row, col] = 255 23 | else: 24 | binary[row, col] = 0 25 | cv.imshow("binary", binary) 26 | 27 | cv.waitKey(0) 28 | cv.destroyAllWindows() 29 | 30 | 31 | -------------------------------------------------------------------------------- /OpenCV_learn/code_040/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_040/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_041/opencv_041.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ת��Ϊ�Ҷ�ͼ�� 18 | int T = 127; 19 | Mat gray, binary; 20 | cvtColor(src, gray, COLOR_BGR2GRAY); 21 | for (int i = 0; i < 5; i++) { 22 | threshold(gray, binary, T, 255, i); 23 | imshow(format("binary_%d" , i), binary); 24 | imwrite(format("./binary_%d.png", i), binary); 25 | } 26 | waitKey(0); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_041/opencv_041.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | # 4 | # THRESH_BINARY = 0 5 | # THRESH_BINARY_INV = 1 6 | # THRESH_TRUNC = 2 7 | # THRESH_TOZERO = 3 8 | # THRESH_TOZERO_INV = 4 9 | # 10 | src = cv.imread("./test.png") 11 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 12 | cv.imshow("input", src) 13 | 14 | T = 127 15 | 16 | # 转换为灰度图像 17 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 18 | for i in range(5): 19 | ret, binary = cv.threshold(gray, T, 255, i) 20 | cv.imshow("binary_" + str(i), binary) 21 | 22 | cv.waitKey(0) 23 | cv.destroyAllWindows() 24 | 25 | 26 | -------------------------------------------------------------------------------- /OpenCV_learn/code_041/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_041/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_042/binary_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_042/binary_result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_042/opencv_042.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // �Զ���ֵѰ�����ֵ�� 18 | Mat gray, binary; 19 | cvtColor(src, gray, COLOR_BGR2GRAY); 20 | double t = threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 21 | printf("threshold : %.2f\n", t); 22 | 23 | 24 | imshow("binary", binary); 25 | imwrite("./binary.png", binary); 26 | 27 | waitKey(0); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_042/opencv_042.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | # 4 | # THRESH_BINARY = 0 5 | # THRESH_BINARY_INV = 1 6 | # THRESH_TRUNC = 2 7 | # THRESH_TOZERO = 3 8 | # THRESH_TOZERO_INV = 4 9 | # 10 | src = cv.imread("./test.png") 11 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 12 | cv.imshow("input", src) 13 | h, w = src.shape[:2] 14 | 15 | # 自动阈值分割 OTSU 16 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 17 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 18 | print("ret :", ret) 19 | cv.imshow("binary", binary) 20 | 21 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 22 | result[0:h,0:w,:] = src 23 | result[0:h,w:2*w,:] = cv.cvtColor(binary, cv.COLOR_GRAY2BGR) 24 | cv.putText(result, "input", (10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 25 | cv.putText(result, "binary, threshold = " + str(ret), (w+10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 26 | cv.imshow("result", result) 27 | cv.imwrite("./binary_result.png", result) 28 | 29 | cv.waitKey(0) 30 | cv.destroyAllWindows() 31 | 32 | 33 | -------------------------------------------------------------------------------- /OpenCV_learn/code_042/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_042/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_043/binary_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_043/binary_result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_043/opencv_043.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // �Զ���ֵѰ�����ֵ�� 18 | Mat gray, binary; 19 | cvtColor(src, gray, COLOR_BGR2GRAY); 20 | double t = threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_TRIANGLE); 21 | printf("threshold : %.2f\n", t); 22 | 23 | 24 | imshow("binary", binary); 25 | imwrite("./binary.png", binary); 26 | 27 | waitKey(0); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_043/opencv_043.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | #import tensorflow as tf 4 | 5 | # tf.enable_eager_execution() 6 | # 7 | # THRESH_BINARY = 0 8 | # THRESH_BINARY_INV = 1 9 | # THRESH_TRUNC = 2 10 | # THRESH_TOZERO = 3 11 | # THRESH_TOZERO_INV = 4 12 | # 13 | src = cv.imread("./test.png") 14 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 15 | cv.imshow("input", src) 16 | h, w = src.shape[:2] 17 | 18 | 19 | # 自动阈值分割 TRIANGLE 20 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 21 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE) 22 | print("ret :", ret) 23 | cv.imshow("binary", binary) 24 | 25 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 26 | result[0:h,0:w,:] = src 27 | result[0:h,w:2*w,:] = cv.cvtColor(binary, cv.COLOR_GRAY2BGR) 28 | cv.putText(result, "input", (10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 29 | cv.putText(result, "binary, threshold = " + str(ret), (w+10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 30 | cv.imshow("result", result) 31 | cv.imwrite("./binary_result.png", result) 32 | 33 | cv.waitKey(0) 34 | cv.destroyAllWindows() 35 | 36 | 37 | -------------------------------------------------------------------------------- /OpenCV_learn/code_043/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_043/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_044/binary_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_044/binary_result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_044/opencv_044.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // �Զ���ֵѰ�����ֵ�� 18 | Mat gray, binary; 19 | cvtColor(src, gray, COLOR_BGR2GRAY); 20 | adaptiveThreshold(gray, binary, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 25, 10); 21 | 22 | imshow("binary", binary); 23 | imwrite("./test.png", binary); 24 | 25 | waitKey(0); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_044/opencv_044.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | # 5 | # THRESH_BINARY = 0 6 | # THRESH_BINARY_INV = 1 7 | # THRESH_TRUNC = 2 8 | # THRESH_TOZERO = 3 9 | # THRESH_TOZERO_INV = 4 10 | # 11 | src = cv.imread("./test.png") 12 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 13 | cv.imshow("input", src) 14 | h, w = src.shape[:2] 15 | 16 | 17 | # 自动阈值分割 TRIANGLE 18 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 19 | binary = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 25, 10) 20 | cv.imshow("binary", binary) 21 | 22 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 23 | result[0:h,0:w,:] = src 24 | result[0:h,w:2*w,:] = cv.cvtColor(binary, cv.COLOR_GRAY2BGR) 25 | cv.putText(result, "input", (10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 26 | cv.putText(result, "adaptive threshold", (w+10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 27 | cv.imshow("result", result) 28 | cv.imwrite("./binary_result.png", result) 29 | 30 | cv.waitKey(0) 31 | cv.destroyAllWindows() 32 | 33 | 34 | -------------------------------------------------------------------------------- /OpenCV_learn/code_044/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_044/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_045/binary_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_045/binary_result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_045/opencv_045.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat dst, gray, binary; 19 | pyrMeanShiftFiltering(src, dst, 10, 100); 20 | // GaussianBlur(src, dst, Size(3, 3), 0, 0); 21 | cvtColor(dst, gray, COLOR_BGR2GRAY); 22 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 23 | imshow("binary", binary); 24 | imwrite("./binary.png", binary); 25 | 26 | waitKey(0); 27 | return 0; 28 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_045/opencv_045.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def method_1(image): 6 | gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) 7 | t, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 8 | return binary 9 | 10 | 11 | def method_2(image): 12 | blurred = cv.GaussianBlur(image, (3, 3), 0) 13 | gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY) 14 | t, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 15 | return binary 16 | 17 | 18 | def method_3(image): 19 | blurred = cv.pyrMeanShiftFiltering(image, 10, 100) 20 | gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY) 21 | t, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 22 | return binary 23 | 24 | 25 | src = cv.imread("./test.png") 26 | h, w = src.shape[:2] 27 | ret = method_3(src) 28 | 29 | result = np.zeros([h, w*2, 3], dtype=src.dtype) 30 | result[0:h,0:w,:] = src 31 | result[0:h,w:2*w,:] = cv.cvtColor(ret, cv.COLOR_GRAY2BGR) 32 | cv.putText(result, "input", (10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 33 | cv.putText(result, "binary", (w+10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2) 34 | cv.imshow("result", result) 35 | cv.imwrite("./binary_result.png", result) 36 | 37 | cv.waitKey(0) 38 | cv.destroyAllWindows() 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /OpenCV_learn/code_045/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_045/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_046/labels.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_046/labels.png -------------------------------------------------------------------------------- /OpenCV_learn/code_046/opencv_046.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | RNG rng(12345); 8 | void connected_component_demo(Mat &image); 9 | int main(int argc, char** argv) { 10 | Mat src = imread("./test.png"); 11 | if (src.empty()) { 12 | printf("could not load image...\n"); 13 | } 14 | imshow("input", src); 15 | connected_component_demo(src); 16 | 17 | waitKey(0); 18 | return 0; 19 | } 20 | 21 | void connected_component_demo(Mat &image) { 22 | // extract labels 23 | Mat gray, binary; 24 | GaussianBlur(image, image, Size(3, 3), 0); 25 | cvtColor(image, gray, COLOR_BGR2GRAY); 26 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 27 | Mat labels = Mat::zeros(image.size(), CV_32S); 28 | int num_labels = connectedComponents(binary, labels, 8, CV_32S); 29 | printf("total labels : %d\n", (num_labels - 1)); 30 | vector colors(num_labels); 31 | 32 | // background color 33 | colors[0] = Vec3b(0, 0, 0); 34 | 35 | // object color 36 | for (int i = 1; i < num_labels; i++) { 37 | colors[i] = Vec3b(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256)); 38 | } 39 | 40 | // render result 41 | Mat dst = Mat::zeros(image.size(), image.type()); 42 | int w = image.cols; 43 | int h = image.rows; 44 | for (int row = 0; row < h; row++) { 45 | for (int col = 0; col < w; col++) { 46 | int label = labels.at(row, col); 47 | if (label == 0) continue; 48 | dst.at(row, col) = colors[label]; 49 | } 50 | } 51 | imshow("ccla-demo", dst); 52 | imwrite("./ccla_dst.png", dst); 53 | } 54 | -------------------------------------------------------------------------------- /OpenCV_learn/code_046/opencv_046.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def connected_components_demo(src): 6 | src = cv.GaussianBlur(src, (3, 3), 0) 7 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 8 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 9 | cv.imshow("binary", binary) 10 | 11 | output = cv.connectedComponents(binary, connectivity=8, ltype=cv.CV_32S) 12 | num_labels = output[0] 13 | labels = output[1] 14 | colors = [] 15 | for i in range(num_labels): 16 | b = np.random.randint(0, 256) 17 | g = np.random.randint(0, 256) 18 | r = np.random.randint(0, 256) 19 | colors.append((b, g, r)) 20 | 21 | colors[0] = (0, 0, 0) 22 | h, w = gray.shape 23 | image = np.zeros((h, w, 3), dtype=np.uint8) 24 | for row in range(h): 25 | for col in range(w): 26 | image[row, col] = colors[labels[row, col]] 27 | 28 | cv.imshow("colored labels", image) 29 | cv.imwrite("./labels.png", image) 30 | print("total rice : ", num_labels - 1) 31 | 32 | 33 | src = cv.imread("./test.png") 34 | h, w = src.shape[:2] 35 | connected_components_demo(src) 36 | cv.waitKey(0) 37 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_046/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_046/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_047/labels.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_047/labels.png -------------------------------------------------------------------------------- /OpenCV_learn/code_047/opencv_047.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def connected_components_stats_demo(src): 6 | src = cv.GaussianBlur(src, (3, 3), 0) 7 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 8 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 9 | cv.imshow("binary", binary) 10 | 11 | num_labels, labels, stats, centers = cv.connectedComponentsWithStats(binary, connectivity=8, ltype=cv.CV_32S) 12 | colors = [] 13 | for i in range(num_labels): 14 | b = np.random.randint(0, 256) 15 | g = np.random.randint(0, 256) 16 | r = np.random.randint(0, 256) 17 | colors.append((b, g, r)) 18 | 19 | colors[0] = (0, 0, 0) 20 | image = np.copy(src) 21 | for t in range(1, num_labels, 1): 22 | x, y, w, h, area = stats[t] 23 | cx, cy = centers[t] 24 | cv.circle(image, (np.int32(cx), np.int32(cy)), 2, (0, 255, 0), 2, 8, 0) 25 | cv.rectangle(image, (x, y), (x+w, y+h), colors[t], 1, 8, 0) 26 | cv.putText(image, "num:" + str(t), (x, y), cv.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 255), 1); 27 | print("label index %d, area of the label : %d"%(t, area)) 28 | 29 | cv.imshow("colored labels", image) 30 | cv.imwrite("./labels.png", image) 31 | print("total rice : ", num_labels - 1) 32 | 33 | 34 | input = cv.imread("./test.png") 35 | connected_components_stats_demo(input) 36 | cv.waitKey(0) 37 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_047/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_047/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_048/opencv_048.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat dst, gray, binary; 19 | GaussianBlur(src, dst, Size(3, 3), 0, 0); 20 | cvtColor(dst, gray, COLOR_BGR2GRAY); 21 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 22 | imshow("binary", binary); 23 | imwrite("./binary.png", binary); 24 | 25 | // ������������� 26 | vector> contours; 27 | vector hierarchy; 28 | findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point()); 29 | for (size_t t = 0; t < contours.size(); t++) { 30 | drawContours(src, contours, t, Scalar(0, 0, 255), 2, 8); 31 | } 32 | imshow("contours", src); 33 | 34 | waitKey(0); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_048/opencv_048.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def threshold_demo(image): 6 | # 去噪声+二值化 7 | dst = cv.GaussianBlur(image,(3, 3), 0) 8 | gray = cv.cvtColor(dst, cv.COLOR_BGR2GRAY) 9 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_OTSU | cv.THRESH_BINARY) 10 | cv.imshow("binary", binary) 11 | return binary 12 | 13 | 14 | def canny_demo(image): 15 | t = 100 16 | canny_output = cv.Canny(image, t, t * 2) 17 | cv.imshow("canny_output", canny_output) 18 | return canny_output 19 | 20 | 21 | src = cv.imread("./test.png") 22 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 23 | cv.imshow("input", src) 24 | binary = threshold_demo(src) 25 | 26 | # 轮廓发现 27 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) 28 | for c in range(len(contours)): 29 | cv.drawContours(src, contours, c, (0, 0, 255), 2, 8) 30 | 31 | # 显示 32 | cv.imshow("contours-demo", src) 33 | 34 | cv.waitKey(0) 35 | cv.destroyAllWindows() 36 | 37 | 38 | -------------------------------------------------------------------------------- /OpenCV_learn/code_048/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_048/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_049/canny_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_049/canny_output.png -------------------------------------------------------------------------------- /OpenCV_learn/code_049/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_049/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_049/lp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_049/lp.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_049/lp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_049/lp.png -------------------------------------------------------------------------------- /OpenCV_learn/code_049/lplp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_049/lplp.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_049/opencv_049.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat dst, gray, binary; 19 | Canny(src, binary, 80, 160, 3, false); 20 | imshow("binary", binary); 21 | imwrite("./binary.png", binary); 22 | 23 | Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 24 | dilate(binary, binary, k); 25 | 26 | // ������������� 27 | vector> contours; 28 | vector hierarchy; 29 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point()); 30 | for (size_t t = 0; t < contours.size(); t++) { 31 | // ���������� 32 | Rect rect = boundingRect(contours[t]); 33 | rectangle(src, rect, Scalar(0, 0, 255), 1, 8, 0); 34 | 35 | // ��С������� 36 | RotatedRect rrt = minAreaRect(contours[t]); 37 | Point2f pts[4]; 38 | rrt.points(pts); 39 | 40 | // ������ת����������λ�� 41 | for (int i = 0; i < 4; i++) { 42 | line(src, pts[i % 4], pts[(i + 1) % 4], Scalar(0, 255, 0), 2, 8, 0); 43 | } 44 | Point2f cpt = rrt.center; 45 | circle(src, cpt, 2, Scalar(255, 0, 0), 2, 8, 0); 46 | } 47 | imshow("contours", src); 48 | 49 | waitKey(0); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_049/opencv_049.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def canny_demo(image): 6 | t = 80 7 | canny_output = cv.Canny(image, t, t * 2) 8 | cv.imshow("canny_output", canny_output) 9 | cv.imwrite("./canny_output.png", canny_output) 10 | return canny_output 11 | 12 | 13 | src = cv.imread("./lp.jpg") 14 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 15 | cv.imshow("input", src) 16 | binary = canny_demo(src) 17 | k = np.ones((3, 3), dtype=np.uint8) 18 | binary = cv.morphologyEx(binary, cv.MORPH_DILATE, k) 19 | 20 | # 轮廓发现 21 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 22 | for c in range(len(contours)): 23 | # x, y, w, h = cv.boundingRect(contours[c]); 24 | # cv.drawContours(src, contours, c, (0, 0, 255), 2, 8) 25 | # cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0); 26 | rect = cv.minAreaRect(contours[c]) 27 | cx, cy = rect[0] 28 | box = cv.boxPoints(rect) 29 | box = np.int0(box) 30 | cv.drawContours(src,[box],0,(0,0,255),2) 31 | cv.circle(src, (np.int32(cx), np.int32(cy)), 2, (255, 0, 0), 2, 8, 0) 32 | 33 | 34 | # 显示 35 | cv.imshow("contours_analysis", src) 36 | cv.imwrite("./contours_analysis.png", src) 37 | cv.waitKey(0) 38 | cv.destroyAllWindows() 39 | 40 | 41 | -------------------------------------------------------------------------------- /OpenCV_learn/code_049/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_049/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_050/canny_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_050/canny_output.png -------------------------------------------------------------------------------- /OpenCV_learn/code_050/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_050/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_050/lp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_050/lp.png -------------------------------------------------------------------------------- /OpenCV_learn/code_050/opencv_050.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread(".zhifang_ball.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat dst, gray, binary; 19 | Canny(src, binary, 80, 160, 3, false); 20 | imshow("binary", binary); 21 | imwrite("./binary.png", binary); 22 | 23 | Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 24 | dilate(binary, binary, k); 25 | 26 | // ������������� 27 | vector> contours; 28 | vector hierarchy; 29 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point()); 30 | for (size_t t = 0; t < contours.size(); t++) { 31 | // ���������� 32 | Rect rect = boundingRect(contours[t]); 33 | // rectangle(src, rect, Scalar(0, 0, 255), 1, 8, 0); 34 | 35 | // ������� 36 | double area = contourArea(contours[t]); 37 | double curvelen = arcLength(contours[t], true); 38 | if (area < 100 || curvelen < 100) { 39 | continue; 40 | } 41 | 42 | // ��С������� 43 | RotatedRect rrt = minAreaRect(contours[t]); 44 | Point2f pts[4]; 45 | rrt.points(pts); 46 | 47 | // ������ת����������λ�� 48 | for (int i = 0; i < 4; i++) { 49 | line(src, pts[i % 4], pts[(i + 1) % 4], Scalar(0, 255, 0), 2, 8, 0); 50 | } 51 | Point2f cpt = rrt.center; 52 | circle(src, cpt, 2, Scalar(255, 0, 0), 2, 8, 0); 53 | } 54 | imshow("contours", src); 55 | 56 | waitKey(0); 57 | return 0; 58 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_050/opencv_050.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def canny_demo(image): 6 | t = 80 7 | canny_output = cv.Canny(image, t, t * 2) 8 | cv.imshow("canny_output", canny_output) 9 | cv.imwrite("./canny_output.png", canny_output) 10 | return canny_output 11 | 12 | 13 | src = cv.imread("./lp.png") 14 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 15 | cv.imshow("input", src) 16 | binary = canny_demo(src) 17 | k = np.ones((3, 3), dtype=np.uint8) 18 | binary = cv.morphologyEx(binary, cv.MORPH_DILATE, k) 19 | 20 | # 轮廓发现 21 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 22 | for c in range(len(contours)): 23 | # x, y, w, h = cv.boundingRect(contours[c]); 24 | # cv.drawContours(src, contours, c, (0, 0, 255), 2, 8) 25 | # cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0); 26 | area = cv.contourArea(contours[c]) 27 | arclen = cv.arcLength(contours[c], True) 28 | if area < 100 or arclen < 100: 29 | continue 30 | rect = cv.minAreaRect(contours[c]) 31 | cx, cy = rect[0] 32 | box = cv.boxPoints(rect) 33 | box = np.int0(box) 34 | cv.drawContours(src,[box],0,(0,0,255),2) 35 | cv.circle(src, (np.int32(cx), np.int32(cy)), 2, (255, 0, 0), 2, 8, 0) 36 | 37 | 38 | # 显示 39 | cv.imshow("contours_analysis", src) 40 | cv.imwrite("./contours_analysis.png", src) 41 | cv.waitKey(0) 42 | cv.destroyAllWindows() 43 | 44 | 45 | -------------------------------------------------------------------------------- /OpenCV_learn/code_050/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_050/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_051/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_051/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_051/opencv_051.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | RNG rng(50000); 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat dst, gray, binary; 19 | cvtColor(src, gray, COLOR_BGR2GRAY); 20 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 21 | imshow("binary", binary); 22 | imwrite("./binary.png", binary); 23 | 24 | // ������������� 25 | vector> contours; 26 | vector hierarchy; 27 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point()); 28 | Scalar color = Scalar(255, 0, 0); 29 | for (size_t t = 0; t < contours.size(); t++) { 30 | RotatedRect rrt = minAreaRect(contours[t]); 31 | Point2f cpt = rrt.center; 32 | circle(src, cpt, 2, Scalar(0, 255, 0), 2, 8, 0); 33 | 34 | Mat result; 35 | approxPolyDP(contours[t], result, 4, true); 36 | printf("corners : %d\n", result.rows); 37 | if (result.rows == 6) { 38 | putText(src, "poly", cpt, FONT_HERSHEY_SIMPLEX, .7, color, 1, 8); 39 | } 40 | if (result.rows == 3) { 41 | putText(src, "triangle", cpt, FONT_HERSHEY_SIMPLEX, .7, color, 1, 8); 42 | } 43 | if (result.rows == 4) { 44 | putText(src, "rectangle", cpt, FONT_HERSHEY_SIMPLEX, .7, color, 1, 8); 45 | } 46 | if(result.rows > 10) { 47 | putText(src, "circle", cpt, FONT_HERSHEY_SIMPLEX, .7, Scalar(0, 255, 0), 1, 8); 48 | } 49 | } 50 | imshow("contours", src); 51 | 52 | waitKey(0); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_051/opencv_051.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 8 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 9 | 10 | # 轮廓发现 11 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 12 | for c in range(len(contours)): 13 | rect = cv.minAreaRect(contours[c]) 14 | cx, cy = rect[0] 15 | result = cv.approxPolyDP(contours[c], 4, True) 16 | vertexes = result.shape[0] 17 | if vertexes == 3: 18 | cv.putText(src, "triangle", (np.int32(cx), np.int32(cy)), 19 | cv.FONT_HERSHEY_SIMPLEX, .7, (0, 0, 255), 2, 8); 20 | if vertexes == 4: 21 | cv.putText(src, "rectangle", (np.int32(cx), np.int32(cy)), 22 | cv.FONT_HERSHEY_SIMPLEX, .7, (0, 0, 255), 2, 8); 23 | if vertexes == 6: 24 | cv.putText(src, "poly", (np.int32(cx), np.int32(cy)), 25 | cv.FONT_HERSHEY_SIMPLEX, .7, (0, 0, 255), 2, 8); 26 | if vertexes > 10: 27 | cv.putText(src, "circle", (np.int32(cx), np.int32(cy)), 28 | cv.FONT_HERSHEY_SIMPLEX, .7, (0, 0, 255), 2, 8); 29 | print(vertexes) 30 | 31 | 32 | # 显示 33 | cv.imshow("contours_analysis", src) 34 | cv.imwrite("./contours_analysis.png", src) 35 | cv.waitKey(0) 36 | cv.destroyAllWindows() 37 | 38 | 39 | -------------------------------------------------------------------------------- /OpenCV_learn/code_051/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_051/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_052/canny_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_052/canny_output.png -------------------------------------------------------------------------------- /OpenCV_learn/code_052/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_052/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_052/opencv_052.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat dst, gray, binary; 19 | Canny(src, binary, 80, 160, 3, false); 20 | imshow("binary", binary); 21 | imwrite("./binary.png", binary); 22 | 23 | Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 24 | dilate(binary, binary, k); 25 | 26 | // ������������� 27 | vector> contours; 28 | vector hierarchy; 29 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point()); 30 | for (size_t t = 0; t < contours.size(); t++) { 31 | // ��С������� 32 | RotatedRect rrt = minAreaRect(contours[t]); 33 | float w = rrt.size.width; 34 | float h = rrt.size.height; 35 | Point2f pts[4]; 36 | rrt.points(pts); 37 | 38 | // ������ݱ� 39 | float ratio = min(w, h) / max(w, h); 40 | Point2f cpt = rrt.center; 41 | circle(src, cpt, 2, Scalar(255, 0, 0), 2, 8, 0); 42 | if (ratio > 0.9) { 43 | circle(src, cpt, 2, Scalar(255, 0, 0), 2, 8, 0); 44 | // ������ת����������λ�� 45 | for (int i = 0; i < 4; i++) { 46 | line(src, pts[i % 4], pts[(i + 1) % 4], Scalar(0, 0, 255), 2, 8, 0); 47 | } 48 | } 49 | if (ratio < 0.5) { 50 | circle(src, cpt, 2, Scalar(255, 0, 0), 2, 8, 0); 51 | // ������ת����������λ�� 52 | for (int i = 0; i < 4; i++) { 53 | line(src, pts[i % 4], pts[(i + 1) % 4], Scalar(0, 255, 0), 2, 8, 0); 54 | } 55 | } 56 | } 57 | imshow("contours", src); 58 | 59 | waitKey(0); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_052/opencv_052.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def canny_demo(image): 6 | t = 80 7 | canny_output = cv.Canny(image, t, t * 2) 8 | cv.imshow("canny_output", canny_output) 9 | cv.imwrite("./canny_output.png", canny_output) 10 | return canny_output 11 | 12 | 13 | src = cv.imread("./test.png") 14 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 15 | cv.imshow("input", src) 16 | binary = canny_demo(src) 17 | k = np.ones((3, 3), dtype=np.uint8) 18 | binary = cv.morphologyEx(binary, cv.MORPH_DILATE, k) 19 | 20 | # 轮廓发现 21 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 22 | for c in range(len(contours)): 23 | rect = cv.minAreaRect(contours[c]) 24 | cx, cy = rect[0] 25 | ww, hh = rect[1] 26 | ratio = np.minimum(ww, hh) / np.maximum(ww, hh) 27 | print(ratio) 28 | mm = cv.moments(contours[c]) 29 | m00 = mm['m00'] 30 | m10 = mm['m10'] 31 | m01 = mm['m01'] 32 | cx = np.int(m10 / m00) 33 | cy = np.int(m01 / m00) 34 | box = cv.boxPoints(rect) 35 | box = np.int0(box) 36 | if ratio > 0.9: 37 | cv.drawContours(src, [box], 0, (0, 0, 255), 2) 38 | cv.circle(src, (np.int32(cx), np.int32(cy)), 2, (255, 0, 0), 2, 8, 0) 39 | if ratio < 0.5: 40 | cv.drawContours(src, [box], 0, (255, 0, 255), 2) 41 | cv.circle(src, (np.int32(cx), np.int32(cy)), 2, (0, 0, 255), 2, 8, 0) 42 | 43 | 44 | # 显示 45 | cv.imshow("contours_analysis", src) 46 | cv.imwrite("./contours_analysis.png", src) 47 | cv.waitKey(0) 48 | cv.destroyAllWindows() 49 | 50 | 51 | -------------------------------------------------------------------------------- /OpenCV_learn/code_052/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_052/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_053/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_053/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_053/opencv_053.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | void contours_info(Mat &image, vector> &pts); 8 | int main(int argc, char** argv) { 9 | Mat src = imread("./test.png"); 10 | imshow("input", src); 11 | Mat src2 = imread("./test.png"); 12 | imshow("input2", src2); 13 | 14 | // ������ȡ 15 | vector> contours1; 16 | vector> contours2; 17 | contours_info(src, contours1); 18 | contours_info(src2, contours2); 19 | // hu�ؼ��� 20 | Moments mm2 = moments(contours2[0]); 21 | Mat hu2; 22 | HuMoments(mm2, hu2); 23 | // ����ƥ�� 24 | for (size_t t = 0; t < contours1.size(); t++) { 25 | Moments mm = moments(contours1[t]); 26 | Mat hum; 27 | HuMoments(mm, hum); 28 | double dist = matchShapes(hum, hu2, CONTOURS_MATCH_I1, 0); 29 | printf("contour match distance : %.2f\n", dist); 30 | if (dist < 1) { 31 | printf("draw it \n"); 32 | drawContours(src, contours1, t, Scalar(0, 0, 255), 2, 8); 33 | } 34 | } 35 | imshow("match result", src); 36 | waitKey(0); 37 | return 0; 38 | } 39 | 40 | void contours_info(Mat &image, vector> &contours) { 41 | Mat gray, binary; 42 | vector hierarchy; 43 | cvtColor(image, gray, COLOR_BGR2GRAY); 44 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 45 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); 46 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_053/opencv_053.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def contours_info(image): 6 | gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) 7 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 8 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 9 | return contours 10 | 11 | 12 | src = cv.imread("./test.png") 13 | cv.namedWindow("input1", cv.WINDOW_AUTOSIZE) 14 | cv.imshow("input1", src) 15 | src2 = cv.imread("./test.png") 16 | cv.imshow("input2", src2) 17 | 18 | # 轮廓发现 19 | contours1 = contours_info(src) 20 | contours2 = contours_info(src2) 21 | 22 | # 几何矩计算与hu矩计算 23 | mm2 = cv.moments(contours2[0]) 24 | hum2 = cv.HuMoments(mm2) 25 | 26 | # 轮廓匹配 27 | for c in range(len(contours1)): 28 | mm = cv.moments(contours1[c]) 29 | hum = cv.HuMoments(mm) 30 | dist = cv.matchShapes(hum, hum2, cv.CONTOURS_MATCH_I1, 0) 31 | if dist < 1: 32 | cv.drawContours(src, contours1, c, (0, 0, 255), 2, 8) 33 | print("dist %f"%(dist)) 34 | 35 | # 显示 36 | cv.imshow("contours_analysis", src) 37 | cv.imwrite("./contours_analysis.png", src) 38 | cv.waitKey(0) 39 | cv.destroyAllWindows() 40 | 41 | 42 | -------------------------------------------------------------------------------- /OpenCV_learn/code_053/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_053/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_054/canny_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_054/canny_output.png -------------------------------------------------------------------------------- /OpenCV_learn/code_054/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_054/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_054/opencv_054.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat dst, gray, binary; 19 | Canny(src, binary, 80, 160, 3, false); 20 | imshow("binary", binary); 21 | imwrite("./binary.png", binary); 22 | 23 | Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 24 | dilate(binary, binary, k); 25 | 26 | // ������������� 27 | vector> contours; 28 | vector hierarchy; 29 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point()); 30 | for (size_t t = 0; t < contours.size(); t++) { 31 | // ��С������� 32 | RotatedRect rrt = fitEllipse(contours[t]); 33 | float w = rrt.size.width; 34 | float h = rrt.size.height; 35 | Point centers = rrt.center; 36 | ellipse(src, rrt, Scalar(0, 0, 255), 2, 8); 37 | } 38 | imshow("contours", src); 39 | 40 | waitKey(0); 41 | return 0; 42 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_054/opencv_054.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def canny_demo(image): 6 | t = 80 7 | canny_output = cv.Canny(image, t, t * 2) 8 | cv.imshow("canny_output", canny_output) 9 | cv.imwrite("./canny_output.png", canny_output) 10 | return canny_output 11 | 12 | 13 | src = cv.imread("./test.png") 14 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 15 | cv.imshow("input", src) 16 | binary = canny_demo(src) 17 | k = np.ones((3, 3), dtype=np.uint8) 18 | binary = cv.morphologyEx(binary, cv.MORPH_DILATE, k) 19 | 20 | # 轮廓发现 21 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 22 | for c in range(len(contours)): 23 | # 椭圆拟合 24 | (cx, cy), (a, b), angle = cv.fitEllipse(contours[c]) 25 | # 绘制椭圆 26 | cv.ellipse(src, (np.int32(cx), np.int32(cy)), 27 | (np.int32(a/2), np.int32(b/2)), angle, 0, 360, (0, 0, 255), 2, 8, 0) 28 | 29 | 30 | # 显示 31 | cv.imshow("contours_analysis", src) 32 | cv.imwrite("./contours_analysis.png", src) 33 | cv.waitKey(0) 34 | cv.destroyAllWindows() 35 | 36 | 37 | -------------------------------------------------------------------------------- /OpenCV_learn/code_054/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_054/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_055/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_055/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_055/opencv_055.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.jpg"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ��ֵ�� 18 | Mat dst, gray, binary; 19 | cvtColor(src, gray, COLOR_BGR2GRAY); 20 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 21 | 22 | // ɾ�����ſ� 23 | Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 24 | morphologyEx(binary, binary, MORPH_OPEN, k); 25 | imshow("binary", binary); 26 | 27 | // ������������� 28 | vector> contours; 29 | vector hierarchy; 30 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point()); 31 | for (size_t t = 0; t < contours.size(); t++) { 32 | vector hull; 33 | convexHull(contours[t], hull); 34 | bool isHull = isContourConvex(contours[t]); 35 | printf("test convex of the contours %s", isHull?"Y":"N"); 36 | int len = hull.size(); 37 | for (int i = 0; i < hull.size(); i++) { 38 | circle(src, hull[i], 4, Scalar(255, 0, 0), 2, 8, 0); 39 | line(src, hull[i%len],hull[(i+1)%len], Scalar(0, 0, 255), 2, 8, 0); 40 | } 41 | } 42 | imshow("contours", src); 43 | 44 | waitKey(0); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_055/opencv_055.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.jpg") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 8 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 9 | k = cv.getStructuringElement(cv.MORPH_RECT, (3, 3)) 10 | binary = cv.morphologyEx(binary, cv.MORPH_OPEN, k) 11 | cv.imshow("binary", binary) 12 | 13 | # 轮廓发现 14 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 15 | for c in range(len(contours)): 16 | ret = cv.isContourConvex(contours[c]) 17 | points = cv.convexHull(contours[c]) 18 | total = len(points) 19 | for i in range(len(points)): 20 | x1, y1 = points[i%total][0] 21 | x2, y2 = points[(i+1)%total][0] 22 | cv.circle(src, (x1, y1), 4, (255, 0, 0), 2, 8, 0) 23 | cv.line(src, (x1, y1), (x2, y2), (0, 0, 255), 2, 8, 0) 24 | print(points) 25 | print("convex : ", ret) 26 | 27 | 28 | # 显示 29 | cv.imshow("contours_analysis", src) 30 | cv.imwrite("./contours_analysis.png", src) 31 | cv.waitKey(0) 32 | cv.destroyAllWindows() 33 | 34 | 35 | -------------------------------------------------------------------------------- /OpenCV_learn/code_055/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_055/test.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_056/canny_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_056/canny_output.png -------------------------------------------------------------------------------- /OpenCV_learn/code_056/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_056/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_056/opencv_056.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat dst, gray, binary; 19 | Canny(src, binary, 80, 160, 3, false); 20 | imshow("binary", binary); 21 | imwrite("./binary.png", binary); 22 | 23 | Mat k = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 24 | dilate(binary, binary, k); 25 | 26 | // ������������� 27 | vector> contours; 28 | vector hierarchy; 29 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point()); 30 | 31 | for (size_t t = 0; t < contours.size(); t++) { 32 | // ���������� 33 | Rect rect = boundingRect(contours[t]); 34 | int m = max(rect.width, rect.height); 35 | if (m < 30) continue; 36 | // ֱ����� 37 | Vec4f oneline; 38 | fitLine(contours[t], oneline, DIST_L1, 0, 0.01, 0.01); 39 | float vx = oneline[0]; 40 | float vy = oneline[1]; 41 | float x0 = oneline[2]; 42 | float y0 = oneline[3]; 43 | 44 | // ֱ�߲���б��k��ؾ�b 45 | float k = vy / vx; 46 | float b = y0 - k*x0; 47 | 48 | // Ѱ��������ֵ�� 49 | int minx = 0, miny = 10000; 50 | int maxx = 0, maxy = 0; 51 | for (int i = 0; i < contours[t].size(); i++) { 52 | Point pt = contours[t][i]; 53 | if (miny > pt.y) { 54 | miny = pt.y; 55 | } 56 | if (maxy < pt.y) { 57 | maxy = pt.y; 58 | } 59 | } 60 | maxx = (maxy - b) / k; 61 | minx = (miny - b) / k; 62 | line(src, Point(maxx, maxy), Point(minx, miny), Scalar(0, 0, 255), 2, 8, 0); 63 | } 64 | imshow("contours", src); 65 | 66 | waitKey(0); 67 | return 0; 68 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_056/opencv_056.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def canny_demo(image): 6 | t = 80 7 | canny_output = cv.Canny(image, t, t * 2) 8 | cv.imwrite("./canny_output.png", canny_output) 9 | return canny_output 10 | 11 | 12 | src = cv.imread("./test.png") 13 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 14 | cv.imshow("input", src) 15 | 16 | binary = canny_demo(src) 17 | k = np.ones((3, 3), dtype=np.uint8) 18 | binary = cv.morphologyEx(binary, cv.MORPH_DILATE, k) 19 | cv.imshow("binary", binary) 20 | 21 | # 轮廓发现 22 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 23 | 24 | # 直线拟合与极值点寻找 25 | for c in range(len(contours)): 26 | x, y, w, h = cv.boundingRect(contours[c]) 27 | m = max(w, h) 28 | if m < 30: 29 | continue 30 | vx, vy, x0, y0 = cv.fitLine(contours[c], cv.DIST_L1, 0, 0.01, 0.01) 31 | k = vy/vx 32 | b = y0 - k*x0 33 | maxx = 0 34 | maxy = 0 35 | miny = 100000 36 | minx = 0 37 | for pt in contours[c]: 38 | px, py = pt[0] 39 | if maxy < py: 40 | maxy = py 41 | if miny > py: 42 | miny = py 43 | maxx = (maxy - b) / k 44 | minx = (miny - b) / k 45 | cv.line(src, (np.int32(maxx), np.int32(maxy)), 46 | (np.int32(minx), np.int32(miny)), (0, 0, 255), 2, 8, 0) 47 | 48 | 49 | # 显示 50 | cv.imshow("contours_analysis", src) 51 | cv.imwrite("./contours_analysis.png", src) 52 | cv.waitKey(0) 53 | cv.destroyAllWindows() 54 | 55 | 56 | -------------------------------------------------------------------------------- /OpenCV_learn/code_056/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_056/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_057/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_057/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_057/opencv_057.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./test.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ��ֵ�� 18 | Mat dst, gray, binary; 19 | cvtColor(src, gray, COLOR_BGR2GRAY); 20 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 21 | 22 | // ������������� 23 | vector> contours; 24 | vector hierarchy; 25 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point()); 26 | int h = src.rows; 27 | int w = src.cols; 28 | Mat image = Mat::zeros(src.size(), CV_32FC3); 29 | for (int row = 0; row < h; row++) { 30 | for (int col = 0; col < w; col++) { 31 | double dist = pointPolygonTest(contours[0], Point(col, row), true); 32 | if (dist == 0) { 33 | image.at(row, col) = Vec3f(255, 255, 255); 34 | } 35 | if (dist > 0) { 36 | image.at(row, col) = Vec3f(255-dist, 0, 0); 37 | } 38 | if (dist < 0) { 39 | image.at(row, col) = Vec3f(0, 0, 255+dist); 40 | } 41 | 42 | } 43 | } 44 | convertScaleAbs(image, image); 45 | image.convertTo(image, CV_8UC3); 46 | imshow("points", image); 47 | 48 | waitKey(0); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_057/opencv_057.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 8 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 9 | cv.imshow("binary", binary) 10 | 11 | # 轮廓发现 12 | image = np.zeros(src.shape, dtype=np.float32) 13 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 14 | h, w = src.shape[:2] 15 | for row in range(h): 16 | for col in range(w): 17 | dist = cv.pointPolygonTest(contours[0], (col, row), True) 18 | if dist == 0: 19 | image[row, col] = (255, 255, 255) 20 | if dist > 0: 21 | image[row, col] = (255-dist, 0, 0) 22 | if dist < 0: 23 | image[row, col] = (0, 0, 255+dist) 24 | 25 | dst = cv.convertScaleAbs(image) 26 | dst = np.uint8(dst) 27 | 28 | # 显示 29 | cv.imshow("contours_analysis", dst) 30 | cv.imwrite("./contours_analysis.png", dst) 31 | cv.waitKey(0) 32 | cv.destroyAllWindows() 33 | 34 | 35 | -------------------------------------------------------------------------------- /OpenCV_learn/code_057/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_057/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_058/opencv_058.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | from __future__ import division 3 | import cv2 as cv 4 | import numpy as np 5 | # Create an image 6 | r = 100 7 | src = np.zeros((4*r, 4*r), dtype=np.uint8) 8 | # Create a sequence of points to make a contour 9 | vert = [None]*6 10 | vert[0] = (3*r//2, int(1.34*r)) 11 | vert[1] = (1*r, 2*r) 12 | vert[2] = (3*r//2, int(2.866*r)) 13 | vert[3] = (5*r//2, int(2.866*r)) 14 | vert[4] = (3*r, 2*r) 15 | vert[5] = (5*r//2, int(1.34*r)) 16 | # Draw it in src 17 | for i in range(6): 18 | cv.line(src, vert[i], vert[(i+1)%6], ( 255 ), 3) 19 | # Get the contours 20 | _, contours, _ = cv.findContours(src, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) 21 | 22 | # Calculate the distances to the contour 23 | raw_dist = np.empty(src.shape, dtype=np.float32) 24 | for i in range(src.shape[0]): 25 | for j in range(src.shape[1]): 26 | raw_dist[i,j] = cv.pointPolygonTest(contours[0], (j,i), True) 27 | 28 | # 获取最大值即内接圆半径,中心点坐标 29 | minVal, maxVal, _, maxDistPt = cv.minMaxLoc(raw_dist) 30 | minVal = abs(minVal) 31 | maxVal = abs(maxVal) 32 | 33 | # Depicting the distances graphically 34 | drawing = np.zeros((src.shape[0], src.shape[1], 3), dtype=np.uint8) 35 | for i in range(src.shape[0]): 36 | for j in range(src.shape[1]): 37 | if raw_dist[i,j] < 0: 38 | drawing[i,j,0] = 255 - abs(raw_dist[i,j]) * 255 / minVal 39 | elif raw_dist[i,j] > 0: 40 | drawing[i,j,2] = 255 - raw_dist[i,j] * 255 / maxVal 41 | else: 42 | drawing[i,j,0] = 255 43 | drawing[i,j,1] = 255 44 | drawing[i,j,2] = 255 45 | 46 | # max inner circle 47 | cv.circle(drawing,maxDistPt, np.int(maxVal),(255,255,255), 1, cv.LINE_8, 0) 48 | cv.imshow('Source', src) 49 | cv.imshow('Distance and inscribed circle', drawing) 50 | 51 | cv.waitKey(0) 52 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_058/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_058/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_059/Mat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_059/Mat.png -------------------------------------------------------------------------------- /OpenCV_learn/code_059/canny_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_059/canny_output.png -------------------------------------------------------------------------------- /OpenCV_learn/code_059/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_059/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_059/opencv_059.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./Mat.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat dst, gray, binary; 19 | Canny(src, binary, 80, 160, 3, false); 20 | imshow("binary", binary); 21 | imwrite("./binary.png", binary); 22 | 23 | // ��׼����ֱ�߼�� 24 | vector lines; 25 | HoughLines(binary, lines, 1, CV_PI / 180, 150, 0, 0); 26 | 27 | // ����ֱ�� 28 | Point pt1, pt2; 29 | for (size_t i = 0; i < lines.size(); i++) 30 | { 31 | float rho = lines[i][0]; 32 | float theta = lines[i][1]; 33 | double a = cos(theta), b = sin(theta); 34 | double x0 = a*rho, y0 = b*rho; 35 | pt1.x = cvRound(x0 + 1000 * (-b)); 36 | pt1.y = cvRound(y0 + 1000 * (a)); 37 | pt2.x = cvRound(x0 - 1000 * (-b)); 38 | pt2.y = cvRound(y0 - 1000 * (a)); 39 | line(src, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA); 40 | } 41 | 42 | imshow("contours", src); 43 | 44 | waitKey(0); 45 | return 0; 46 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_059/opencv_059.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def canny_demo(image): 6 | t = 80 7 | canny_output = cv.Canny(image, t, t * 2) 8 | cv.imwrite("./canny_output.png", canny_output) 9 | return canny_output 10 | 11 | 12 | src = cv.imread("./Mat.png") 13 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 14 | cv.imshow("input", src) 15 | 16 | binary = canny_demo(src) 17 | cv.imshow("binary", binary) 18 | 19 | lines = cv.HoughLines(binary, 1, np.pi / 180, 150, None, 0, 0) 20 | if lines is not None: 21 | for i in range(0, len(lines)): 22 | rho = lines[i][0][0] 23 | theta = lines[i][0][1] 24 | a = np.cos(theta) 25 | b = np.sin(theta) 26 | x0 = a * rho 27 | y0 = b * rho 28 | pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a))) 29 | pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a))) 30 | cv.line(src, pt1, pt2, (0, 0, 255), 3, cv.LINE_AA) 31 | 32 | 33 | # 显示 34 | cv.imshow("hough line demo", src) 35 | cv.imwrite("./contours_analysis.png", src) 36 | cv.waitKey(0) 37 | cv.destroyAllWindows() 38 | 39 | 40 | -------------------------------------------------------------------------------- /OpenCV_learn/code_059/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_059/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_060/Mat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_060/Mat.png -------------------------------------------------------------------------------- /OpenCV_learn/code_060/canny_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_060/canny_output.png -------------------------------------------------------------------------------- /OpenCV_learn/code_060/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_060/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_060/opencv_060.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, const char *argv[]) 8 | { 9 | Mat src = imread("./Mat.png"); 10 | if (src.empty()) { 11 | printf("could not load image...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | imshow("input", src); 16 | 17 | // ȥ�������ֵ�� 18 | Mat gray, binary; 19 | Canny(src, binary, 80, 160, 3, false); 20 | imshow("binary", binary); 21 | imwrite("./binary.png", binary); 22 | 23 | vector lines; 24 | HoughLinesP(binary, lines, 1, CV_PI / 180, 80, 30, 10); 25 | Mat result = Mat::zeros(src.size(), src.type()); 26 | for (size_t i = 0; i < lines.size(); i++) 27 | { 28 | line(result, Point(lines[i][0], lines[i][1]), 29 | Point(lines[i][2], lines[i][3]), Scalar(0, 255, 0), 1, 8); 30 | } 31 | imshow("contours", result); 32 | 33 | waitKey(0); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_060/opencv_060.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def canny_demo(image): 6 | t = 80 7 | canny_output = cv.Canny(image, t, t * 2) 8 | cv.imwrite("./canny_output.png", canny_output) 9 | return canny_output 10 | 11 | 12 | src = cv.imread("./Mat.png") 13 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 14 | cv.imshow("input", src) 15 | binary = canny_demo(src) 16 | cv.imshow("binary", binary) 17 | 18 | linesP = cv.HoughLinesP(binary, 1, np.pi / 180, 50, None, 50, 10) 19 | if linesP is not None: 20 | for i in range(0, len(linesP)): 21 | l = linesP[i][0] 22 | cv.line(src, (l[0], l[1]), (l[2], l[3]), (255, 0, 0), 1, cv.LINE_AA) 23 | 24 | # 显示 25 | cv.imshow("hough line demo", src) 26 | cv.imwrite("./contours_analysis.png", src) 27 | cv.waitKey(0) 28 | cv.destroyAllWindows() 29 | 30 | 5555 -------------------------------------------------------------------------------- /OpenCV_learn/code_061/contours_analysis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_061/contours_analysis.png -------------------------------------------------------------------------------- /OpenCV_learn/code_061/opencv_061.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat gray; 9 | int dp = 2; // �������������ֲ��������¡�dp��ȡֵԽ�ߣ�Խ���׼�⵽Բ�� 10 | int min_radius = 20; 11 | int max_radius = 100; 12 | int minDist = 10; 13 | Mat src = imread("./coins.jpg"); 14 | imshow("input", src); 15 | cvtColor(src, gray, COLOR_BGR2GRAY); 16 | GaussianBlur(gray, gray, Size(9, 9), 2, 2); 17 | vector circles; 18 | HoughCircles(gray, circles, HOUGH_GRADIENT, dp, minDist, 100, 100, min_radius, max_radius); 19 | for (size_t i = 0; i < circles.size(); i++) 20 | { 21 | Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); 22 | int radius = cvRound(circles[i][2]); 23 | // ����Բ 24 | circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0); 25 | circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0); 26 | } 27 | namedWindow("circles", 1); 28 | imshow("circles", src); 29 | waitKey(0); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_061/opencv_061.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | src = cv.imread("./test.png") 6 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 7 | cv.imshow("input", src) 8 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 9 | gray = cv.GaussianBlur(gray, (9, 9), 2, 2) 10 | dp = 2 11 | param1 = 100 12 | param2 = 80 13 | 14 | 15 | circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, dp, 10, None, param1, param2, 20, 100) 16 | for c in circles[0,:]: 17 | print(c) 18 | cx, cy, r = c 19 | cv.circle(src, (cx, cy), 2, (0, 255, 0), 2, 8, 0) 20 | cv.circle(src, (cx, cy), r, (0, 0, 255), 2, 8, 0) 21 | 22 | 23 | # 显示 24 | cv.imshow("hough line demo", src) 25 | cv.imwrite("./contours_analysis.png", src) 26 | cv.waitKey(0) 27 | cv.destroyAllWindows() 28 | 29 | 30 | -------------------------------------------------------------------------------- /OpenCV_learn/code_061/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_061/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_062/dilate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_062/dilate.png -------------------------------------------------------------------------------- /OpenCV_learn/code_062/erode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_062/erode.png -------------------------------------------------------------------------------- /OpenCV_learn/code_062/opencv_062.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat dresult, eresult; 9 | Mat src = imread("./test.jpg"); 10 | imshow("input", src); 11 | // ����ṹԪ�� 3x3��С���� 12 | Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 13 | // ���� 14 | dilate(src, dresult, se, Point(-1, -1), 1, 0); 15 | // ��ʴ 16 | erode(src, eresult, se, Point(-1, -1), 1, 0); 17 | 18 | // ��ʾ 19 | imshow("dilate", dresult); 20 | imshow("erode", eresult); 21 | waitKey(0); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_062/opencv_062.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 使用3x3结构元素进行膨胀与腐蚀操作 9 | se = np.ones((3, 3), dtype=np.uint8) 10 | dilate = cv.dilate(src, se, None, (-1, -1), 1) 11 | erode = cv.erode(src, se, None, (-1, -1), 1) 12 | 13 | # 显示 14 | cv.imshow("dilate", dilate) 15 | cv.imshow("erode", erode) 16 | cv.imwrite("./dilate.png", dilate) 17 | cv.imwrite("./erode.png", erode) 18 | cv.waitKey(0) 19 | cv.destroyAllWindows() 20 | -------------------------------------------------------------------------------- /OpenCV_learn/code_062/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_062/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_063/dilate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_063/dilate.png -------------------------------------------------------------------------------- /OpenCV_learn/code_063/erode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_063/erode.png -------------------------------------------------------------------------------- /OpenCV_learn/code_063/opencv_063.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat dresult, eresult; 9 | Mat src = imread("./test.png"); 10 | 11 | // ��ֵͼ�� 12 | Mat gray, binary; 13 | cvtColor(src, gray, COLOR_BGR2GRAY); 14 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 15 | imshow("binary", binary); 16 | 17 | // ����ṹԪ�� 3x3��С���� 18 | Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 19 | // ���� 20 | dilate(binary, dresult, se, Point(-1, -1), 1, 0); 21 | // ��ʴ 22 | erode(binary, eresult, se, Point(-1, -1), 1, 0); 23 | 24 | // ��ʾ 25 | imshow("dilate", dresult); 26 | imshow("erode", eresult); 27 | waitKey(0); 28 | return 0; 29 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_063/opencv_063.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | 7 | # 二值化图像 8 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 9 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 10 | cv.imshow("input", binary) 11 | 12 | # 使用3x3结构元素进行膨胀与腐蚀操作 13 | se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1)) 14 | dilate = cv.dilate(binary, se, None, (-1, -1), 1) 15 | erode = cv.erode(binary, se, None, (-1, -1), 1) 16 | 17 | # 显示 18 | cv.imshow("dilate", dilate) 19 | cv.imshow("erode", erode) 20 | cv.imwrite("./dilate.png", dilate) 21 | cv.imwrite("./erode.png", erode) 22 | cv.waitKey(0) 23 | cv.destroyAllWindows() 24 | -------------------------------------------------------------------------------- /OpenCV_learn/code_063/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_063/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_064/opencv_064.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat dresult, eresult; 9 | Mat src = imread("./test.png"); 10 | 11 | // ��ֵͼ�� 12 | Mat gray, binary, result; 13 | cvtColor(src, gray, COLOR_BGR2GRAY); 14 | GaussianBlur(gray, gray, Size(9, 9), 0, 0); 15 | adaptiveThreshold(gray, binary, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY_INV, 45, 15); 16 | imshow("binary", binary); 17 | 18 | // ����ṹԪ�� 5x5��С���� 19 | Mat se = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1)); 20 | morphologyEx(binary, result, MORPH_OPEN, se); 21 | 22 | // ��ʾ 23 | imshow("open demo", result); 24 | waitKey(0); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_064/opencv_064.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 高斯模糊去噪声 9 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 10 | dst = cv.GaussianBlur(gray, (9, 9), 2, 2) 11 | binary = cv.adaptiveThreshold(dst, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, 12 | cv.THRESH_BINARY_INV, 45, 15) 13 | 14 | # 开操作 15 | se = cv.getStructuringElement(cv.MORPH_RECT, (5, 5), (-1, -1)) 16 | binary = cv.morphologyEx(binary, cv.MORPH_OPEN, se) 17 | 18 | cv.imshow("binary", binary) 19 | 20 | cv.waitKey(0) 21 | cv.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /OpenCV_learn/code_064/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_064/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_065/binary1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_065/binary1.png -------------------------------------------------------------------------------- /OpenCV_learn/code_065/binary2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_065/binary2.png -------------------------------------------------------------------------------- /OpenCV_learn/code_065/opencv_065.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat dresult, eresult; 9 | Mat src = imread("./test.png"); 10 | 11 | // ��ֵͼ�� 12 | Mat gray, binary, result; 13 | cvtColor(src, gray, COLOR_BGR2GRAY); 14 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 15 | imshow("binary", binary); 16 | 17 | // ����ṹԪ�� 5x5��С���� 18 | Mat se1 = getStructuringElement(MORPH_RECT, Size(25, 5), Point(-1, -1)); 19 | Mat se2 = getStructuringElement(MORPH_RECT, Size(5, 25), Point(-1, -1)); 20 | morphologyEx(binary, result, MORPH_CLOSE, se1); 21 | morphologyEx(result, result, MORPH_CLOSE, se2); 22 | 23 | // ��ʾ 24 | imshow("open demo", result); 25 | waitKey(0); 26 | return 0; 27 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_065/opencv_065.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 高斯模糊去噪声 9 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 10 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 11 | cv.imwrite("./binary1.png", binary) 12 | cv.imshow("binary1", binary) 13 | 14 | # 开操作 15 | se1 = cv.getStructuringElement(cv.MORPH_RECT, (25, 5), (-1, -1)) 16 | se2 = cv.getStructuringElement(cv.MORPH_RECT, (5, 25), (-1, -1)) 17 | binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, se1) 18 | binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, se2) 19 | 20 | cv.imshow("binary", binary) 21 | cv.imwrite("./binary2.png", binary) 22 | 23 | cv.waitKey(0) 24 | cv.destroyAllWindows() 25 | -------------------------------------------------------------------------------- /OpenCV_learn/code_065/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_065/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_066/close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_066/close.png -------------------------------------------------------------------------------- /OpenCV_learn/code_066/opencv_066.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | void open_demo(); 7 | void close_demo(); 8 | int main(int argc, char** argv) 9 | { 10 | close_demo(); 11 | waitKey(0); 12 | return 0; 13 | } 14 | 15 | void open_demo() { 16 | Mat dresult, eresult; 17 | Mat src = imread("./test.png"); 18 | imshow("input", src); 19 | 20 | // ��ֵͼ�� 21 | Mat gray, binary; 22 | cvtColor(src, gray, COLOR_BGR2GRAY); 23 | threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); 24 | imshow("binary", binary); 25 | 26 | // ����ṹԪ�� 27 | Mat se = getStructuringElement(MORPH_RECT, Size(20, 1), Point(-1, -1)); 28 | morphologyEx(binary, binary, MORPH_OPEN, se); 29 | 30 | vector> contours; 31 | vector hierarhy; 32 | findContours(binary, contours, hierarhy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(-1, -1)); 33 | for (size_t t = 0; t < contours.size(); t++) { 34 | Rect roi = boundingRect(contours[t]); 35 | roi.y = roi.y - 10; 36 | roi.height = 12; 37 | rectangle(src, roi, Scalar(0, 0, 255), 1, 8, 0); 38 | } 39 | // ��ʾ 40 | imshow("open demo", src); 41 | } 42 | 43 | void close_demo() { 44 | Mat src = imread("./test.png"); 45 | namedWindow("input", WINDOW_AUTOSIZE); 46 | imshow("input", src); 47 | 48 | // ͼ���ֵ�� 49 | Mat gray, binary; 50 | cvtColor(src, gray, COLOR_BGR2GRAY); 51 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 52 | 53 | // �ղ��� 54 | // Mat se = getStructuringElement(MORPH_ELLIPSE, Size(15, 15), Point(-1, -1)); 55 | Mat se = getStructuringElement(MORPH_RECT, Size(25, 25), Point(-1, -1)); 56 | morphologyEx(binary, binary, MORPH_CLOSE, se); 57 | imwrite("D:/close.png", binary); 58 | imshow("close", binary); 59 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_066/opencv_066.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | 5 | def open_demo(): 6 | src = cv.imread("./test.png") 7 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 8 | cv.imshow("input", src) 9 | 10 | # 图像二值化 11 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 12 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU) 13 | cv.imwrite("./binary1.png", binary) 14 | cv.imshow("binary1", binary) 15 | 16 | # 开操作 17 | se1 = cv.getStructuringElement(cv.MORPH_RECT, (20, 1), (-1, -1)) 18 | binary = cv.morphologyEx(binary, cv.MORPH_OPEN, se1) 19 | cv.imshow("binary", binary) 20 | cv.imwrite("./binary2.png", binary) 21 | 22 | # 提取轮廓 23 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 24 | for c in range(len(contours)): 25 | x, y, w, h = cv.boundingRect(contours[c]) 26 | y = y - 10 27 | h = 12 28 | cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0) 29 | cv.imshow("result", src) 30 | 31 | 32 | def close_demo(): 33 | src = cv.imread("./test.png") 34 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 35 | cv.imshow("input", src) 36 | 37 | # 图像二值化 38 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 39 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 40 | 41 | # 闭操作 42 | se = cv.getStructuringElement(cv.MORPH_ELLIPSE, (15, 15), (-1, -1)) 43 | binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, se) 44 | cv.imwrite("./close.png", binary) 45 | cv.imshow("close", binary) 46 | 47 | 48 | close_demo() 49 | cv.waitKey(0) 50 | cv.destroyAllWindows() 51 | -------------------------------------------------------------------------------- /OpenCV_learn/code_066/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_066/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_067/binary2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_067/binary2.png -------------------------------------------------------------------------------- /OpenCV_learn/code_067/opencv_067.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat dresult, eresult; 9 | Mat src = imread("./test.png"); 10 | 11 | // ��ֵͼ�� 12 | Mat gray, binary, result; 13 | cvtColor(src, gray, COLOR_BGR2GRAY); 14 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 15 | imshow("binary", binary); 16 | 17 | // ����ṹԪ�� 18 | Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 19 | morphologyEx(binary, result, MORPH_TOPHAT, se); 20 | 21 | // ��ʾ 22 | imshow("tophat demo", result); 23 | waitKey(0); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_067/opencv_067.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 高斯模糊去噪声 9 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 10 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 11 | 12 | # 顶帽操作 13 | se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1)) 14 | binary = cv.morphologyEx(binary, cv.MORPH_TOPHAT, se) 15 | 16 | 17 | cv.imshow("binary", binary) 18 | cv.imwrite("./binary2.png", binary) 19 | 20 | cv.waitKey(0) 21 | cv.destroyAllWindows() 22 | -------------------------------------------------------------------------------- /OpenCV_learn/code_067/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_067/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_068/opencv_068.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat dresult, eresult; 9 | Mat src = imread("./test.png"); 10 | 11 | // ��ֵͼ�� 12 | Mat gray, binary, result; 13 | cvtColor(src, gray, COLOR_BGR2GRAY); 14 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 15 | imshow("binary", binary); 16 | 17 | // ����ṹԪ�� 18 | Mat se = getStructuringElement(MORPH_RECT, Size(9, 9), Point(-1, -1)); 19 | // ��ñ���� 20 | morphologyEx(binary, result, MORPH_BLACKHAT, se); 21 | 22 | // ��ʾ 23 | imshow("tophat demo", result); 24 | waitKey(0); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_068/opencv_068.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 图像二值化 9 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 10 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 11 | 12 | # 黑帽操作 13 | se = cv.getStructuringElement(cv.MORPH_RECT, (9, 9), (-1, -1)) 14 | binary = cv.morphologyEx(binary, cv.MORPH_BLACKHAT, se) 15 | 16 | 17 | cv.imshow("black hat", binary) 18 | cv.imwrite("./binary2.png", binary) 19 | 20 | cv.waitKey(0) 21 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_068/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_068/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_069/external.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_069/external.png -------------------------------------------------------------------------------- /OpenCV_learn/code_069/gradient.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_069/gradient.png -------------------------------------------------------------------------------- /OpenCV_learn/code_069/interal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_069/interal.png -------------------------------------------------------------------------------- /OpenCV_learn/code_069/opencv_069.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | src = cv.imread("./test.png") 4 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 5 | cv.imshow("input", src) 6 | 7 | # 形态学梯度 - 基本梯度 8 | se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1)) 9 | basic = cv.morphologyEx(src, cv.MORPH_GRADIENT, se) 10 | cv.imshow("basic gradient", basic) 11 | 12 | # 外梯度 13 | dilate = cv.morphologyEx(src, cv.MORPH_DILATE, se) 14 | exteral = cv.subtract(dilate, src) 15 | cv.imshow("external gradient", exteral) 16 | 17 | # 内梯度 18 | erode = cv.morphologyEx(src, cv.MORPH_ERODE, se) 19 | interal = cv.subtract(src, erode) 20 | cv.imshow("interal gradient", interal) 21 | 22 | cv.imwrite("./gradient.png", basic) 23 | cv.imwrite("./external.png", exteral) 24 | cv.imwrite("./interal.png", interal) 25 | cv.waitKey(0) 26 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_069/opencv_69.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat basic, exter, inter; 9 | Mat src = imread("./test.png"); 10 | imshow("input", src); 11 | 12 | // ����ṹԪ�� 13 | Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 14 | 15 | // �����ݶ� 16 | morphologyEx(src, basic, MORPH_GRADIENT, se); 17 | imshow("basic gradient", basic); 18 | 19 | // ���ݶ� 20 | morphologyEx(src, exter, MORPH_DILATE, se); 21 | subtract(exter, src, exter); 22 | imshow("external gradient", exter); 23 | 24 | // ���ݶ� 25 | morphologyEx(src, inter, MORPH_ERODE, se); 26 | subtract(src, inter, inter); 27 | imshow("internal gradient", exter); 28 | 29 | waitKey(0); 30 | return 0; 31 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_069/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_069/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_070/opencv_070.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat src = imread("./test.png"); 9 | namedWindow("input", WINDOW_AUTOSIZE); 10 | imshow("input", src); 11 | 12 | // ��̬ѧ�ݶ� - �����ݶ� 13 | Mat basic, gray, binary; 14 | Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 15 | morphologyEx(src, basic, MORPH_GRADIENT, se); 16 | imshow("basic gradient", basic); 17 | 18 | cvtColor(basic, gray, COLOR_BGR2GRAY); 19 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 20 | imshow("binary", binary); 21 | 22 | se = getStructuringElement(MORPH_RECT, Size(1, 5), Point(-1, -1)); 23 | morphologyEx(binary, binary, MORPH_DILATE, se); 24 | vector> contours; 25 | vector hierarchy; 26 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); 27 | for (size_t c = 0; c < contours.size(); c++) { 28 | Rect rect = boundingRect(contours[c]); 29 | double area = contourArea(contours[c]); 30 | if (area < 200) 31 | continue; 32 | int h = rect.height; 33 | int w = rect.width; 34 | if (h >(3 * w) || h < 20) 35 | continue; 36 | rectangle(src, rect, Scalar(0, 0, 255), 1, 8, 0); 37 | } 38 | imshow("result", src); 39 | waitKey(0); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_070/opencv_070.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | src = cv.imread("./test.png") 4 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 5 | cv.imshow("input", src) 6 | 7 | # 形态学梯度 - 基本梯度 8 | se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1)) 9 | basic = cv.morphologyEx(src, cv.MORPH_GRADIENT, se) 10 | cv.imshow("basic gradient", basic) 11 | 12 | gray = cv.cvtColor(basic, cv.COLOR_BGR2GRAY) 13 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 14 | cv.imshow("binary", binary) 15 | 16 | se = cv.getStructuringElement(cv.MORPH_RECT, (1, 5), (-1, -1)) 17 | binary = cv.morphologyEx(binary, cv.MORPH_DILATE, se) 18 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 19 | for c in range(len(contours)): 20 | x, y, w, h = cv.boundingRect(contours[c]) 21 | area = cv.contourArea(contours[c]) 22 | if area < 200: 23 | continue 24 | if h > (3*w) or h < 20: 25 | continue 26 | cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0) 27 | 28 | cv.imshow("result", src) 29 | cv.waitKey(0) 30 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_070/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_070/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_071/binary2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_071/binary2.png -------------------------------------------------------------------------------- /OpenCV_learn/code_071/opencv_071.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat dresult, eresult; 9 | Mat src = imread("./test.png"); 10 | 11 | // ��ֵͼ�� 12 | Mat gray, binary, result; 13 | cvtColor(src, gray, COLOR_BGR2GRAY); 14 | threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); 15 | imshow("binary", binary); 16 | 17 | // ����ṹԪ�� 18 | Mat se = getStructuringElement(MORPH_CROSS, Size(11, 11), Point(-1, -1)); 19 | // ���л����� 20 | morphologyEx(binary, result, MORPH_HITMISS, se); 21 | 22 | // ��ʾ 23 | imshow("hit-and-miss demo", result); 24 | waitKey(0); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_071/opencv_071.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 图像二值化 9 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 10 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU) 11 | 12 | # 击中击不中 13 | se = cv.getStructuringElement(cv.MORPH_CROSS, (11, 11), (-1, -1)) 14 | binary = cv.morphologyEx(binary, cv.MORPH_HITMISS, se) 15 | 16 | 17 | cv.imshow("black hat", binary) 18 | cv.imwrite("./binary2.png", binary) 19 | 20 | cv.waitKey(0) 21 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_071/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_071/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_072/binary2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_072/binary2.png -------------------------------------------------------------------------------- /OpenCV_learn/code_072/opencv_072.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat src = imread("./test.jpg"); 9 | imshow("input", src); 10 | 11 | // ��ֵͼ�� 12 | Mat gray, binary, result; 13 | cvtColor(src, gray, COLOR_BGR2GRAY); 14 | threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); 15 | imshow("binary", binary); 16 | 17 | // ����ṹԪ�� 18 | Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 19 | morphologyEx(binary, binary, MORPH_OPEN, se); 20 | 21 | vector> contours; 22 | vector hierarchy; 23 | findContours(binary, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE); 24 | int height = src.rows; 25 | for (size_t t = 0; t < contours.size(); t++) { 26 | Rect rect = boundingRect(contours[t]); 27 | double area = contourArea(contours[t]); 28 | if (rect.height > (height / 2)) { 29 | continue; 30 | } 31 | if (area < 150) 32 | continue; 33 | rectangle(src, rect, Scalar(0, 0, 255), 1, 8, 0); 34 | drawContours(src, contours, t, Scalar(0, 255, 0), 2, 8); 35 | } 36 | // ��ʾ 37 | imshow("result", src); 38 | waitKey(0); 39 | return 0; 40 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_072/opencv_072.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.jpg") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 图像二值化 9 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 10 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU) 11 | 12 | se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1)) 13 | binary = cv.morphologyEx(binary, cv.MORPH_OPEN, se) 14 | cv.imshow("binary", binary) 15 | 16 | # 轮廓提取 17 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE) 18 | height, width = src.shape[:2] 19 | for c in range(len(contours)): 20 | x, y, w, h = cv.boundingRect(contours[c]) 21 | area = cv.contourArea(contours[c]) 22 | if h > (height//2): 23 | continue 24 | if area < 150: 25 | continue 26 | cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0) 27 | cv.drawContours(src, contours, c, (0, 255, 0), 2, 8) 28 | 29 | cv.imshow("result", src) 30 | cv.imwrite("./binary2.png", src) 31 | 32 | cv.waitKey(0) 33 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_072/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_072/test.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_073/binary2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_073/binary2.png -------------------------------------------------------------------------------- /OpenCV_learn/code_073/mask1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_073/mask1.png -------------------------------------------------------------------------------- /OpenCV_learn/code_073/mask2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_073/mask2.png -------------------------------------------------------------------------------- /OpenCV_learn/code_073/mask3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_073/mask3.png -------------------------------------------------------------------------------- /OpenCV_learn/code_073/mask4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_073/mask4.png -------------------------------------------------------------------------------- /OpenCV_learn/code_073/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_073/test.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_074/opencv_074.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace cv; 5 | using namespace std; 6 | int main(int argc, char** argv) 7 | { 8 | Mat src = imread("./test.png"); 9 | imshow("input", src); 10 | 11 | // ��ֵͼ�� 12 | Mat gray, binary; 13 | cvtColor(src, gray, COLOR_BGR2GRAY); 14 | threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU); 15 | imshow("binary", binary); 16 | 17 | // ����ṹԪ�� 18 | Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 19 | morphologyEx(binary, binary, MORPH_CLOSE, se); 20 | 21 | vector> contours; 22 | vector hierarchy; 23 | findContours(binary, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE); 24 | int height = src.rows; 25 | int width = src.cols; 26 | int index = -1; 27 | int max = 0; 28 | for (size_t t = 0; t < contours.size(); t++) { 29 | Rect rect = boundingRect(contours[t]); 30 | if (rect.height >= height || rect.width >= width) { 31 | continue; 32 | } 33 | double area = contourArea(contours[t]); 34 | if (area > max) { 35 | max = area; 36 | index = t; 37 | } 38 | } 39 | Mat result = Mat::zeros(src.size(), src.type()); 40 | Mat pts; 41 | drawContours(src, contours, index, Scalar(0, 0, 255), 1, 8); 42 | approxPolyDP(contours[index], pts, 4, true); 43 | for (int i = 0; i < pts.rows; i++) { 44 | Vec2i pt = pts.at(i, 0); 45 | circle(src, Point(pt[0], pt[1]), 2, Scalar(0, 255, 0), 2, 8, 0); 46 | circle(result, Point(pt[0], pt[1]), 2, Scalar(0, 255, 0), 2, 8, 0); 47 | } 48 | imshow("result", src); 49 | waitKey(0); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_074/opencv_074.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.png") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 图像二值化 9 | # src = cv.GaussianBlur(src, (5, 5), 0) 10 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 11 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) 12 | 13 | se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1)) 14 | binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, se) 15 | cv.imshow("binary", binary) 16 | 17 | # 轮廓提取 18 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE) 19 | height, width = src.shape[:2] 20 | index = 0 21 | max = 0 22 | for c in range(len(contours)): 23 | x, y, w, h = cv.boundingRect(contours[c]) 24 | if h >=height or w >= width: 25 | continue 26 | area = cv.contourArea(contours[c]) 27 | if area > max: 28 | max = area 29 | index = c 30 | 31 | # 绘制轮廓关键点与轮廓 32 | result = np.zeros(src.shape, dtype=np.uint8) 33 | keypts = cv.approxPolyDP(contours[index], 4, True) 34 | cv.drawContours(src, contours, index, (0, 0, 255), 1, 8) 35 | cv.drawContours(result, contours, index, (0, 0, 255), 1, 8) 36 | print(keypts) 37 | for pt in keypts: 38 | cv.circle(src, (pt[0][0], pt[0][1]), 2, (0, 255, 0), 2, 8, 0) 39 | cv.circle(result, (pt[0][0], pt[0][1]), 2, (0, 255, 0), 2, 8, 0) 40 | cv.imshow("result", result) 41 | cv.imshow("output", src) 42 | cv.imwrite("./result.png", result) 43 | cv.imwrite("./output.png", src) 44 | 45 | cv.waitKey(0) 46 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_074/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_074/output.png -------------------------------------------------------------------------------- /OpenCV_learn/code_074/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_074/result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_074/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_074/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_075/mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_075/mask.png -------------------------------------------------------------------------------- /OpenCV_learn/code_075/opencv_075.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace cv; 4 | using namespace std; 5 | int main(int argc, char** argv) 6 | { 7 | Mat hsv, mask, result; 8 | Mat src = imread("./test.png"); 9 | if (src.empty()) { 10 | printf("could not load image...\n"); 11 | return -1; 12 | } 13 | cvtColor(src, hsv, COLOR_BGR2HSV); 14 | 15 | // mask 16 | inRange(hsv, Scalar(100, 43, 46), Scalar(124, 255, 255), mask); 17 | Mat se = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1)); 18 | dilate(mask, mask, se); 19 | imshow("mask", mask); 20 | 21 | // �޸� 22 | inpaint(src, mask, result, 3, INPAINT_TELEA); 23 | imshow("result", result); 24 | waitKey(0); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_075/opencv_075.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | if __name__ == '__main__': 4 | src = cv.imread("./test.png") 5 | cv.imshow("watermark image", src) 6 | hsv = cv.cvtColor(src, cv.COLOR_BGR2HSV) 7 | mask = cv.inRange(hsv, (100, 43, 46), (124, 255, 255)) 8 | cv.imshow("mask", mask) 9 | cv.imwrite("./mask.png", mask) 10 | se = cv.getStructuringElement(cv.MORPH_RECT, (5, 5)) 11 | cv.dilate(mask, se, mask) 12 | result = cv.inpaint(src, mask, 3, cv.INPAINT_TELEA) 13 | cv.imshow("result", result) 14 | cv.imwrite("./result.png", result) 15 | cv.waitKey(0) 16 | cv.destroyAllWindows() 17 | -------------------------------------------------------------------------------- /OpenCV_learn/code_075/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_075/result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_075/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_075/test.png -------------------------------------------------------------------------------- /OpenCV_learn/code_076/binary.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_076/binary.png -------------------------------------------------------------------------------- /OpenCV_learn/code_076/opencv_076.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace cv; 4 | using namespace std; 5 | 6 | int main(int argc, char** argv) 7 | { 8 | Mat src = imread("./test.jpg"); 9 | imshow("input", src); 10 | 11 | // ��ֵͼ�� 12 | Mat gray, binary; 13 | cvtColor(src, gray, COLOR_BGR2GRAY); 14 | threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); 15 | imshow("binary", binary); 16 | 17 | // ����ṹԪ�� 18 | Mat se = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); 19 | morphologyEx(binary, binary, MORPH_OPEN, se); 20 | 21 | // Ѱ��������� 22 | vector> contours; 23 | vector hierarchy; 24 | findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); 25 | int index = -1; 26 | int max = 0; 27 | for (size_t t = 0; t < contours.size(); t++) { 28 | double area = contourArea(contours[t]); 29 | if (area > max) { 30 | max = area; 31 | index = t; 32 | } 33 | } 34 | // drawContours(src, contours, index, Scalar(0, 0, 255), 2, 8); 35 | imshow("src", src); 36 | 37 | // Ѱ����С��Ӿ��� 38 | RotatedRect rect = minAreaRect(contours[index]); 39 | int width = static_cast(rect.size.height); 40 | int height = static_cast(rect.size.width); 41 | printf("height %d, width :%d\n", height, width); 42 | 43 | Point2f vertices[4]; 44 | rect.points(vertices); 45 | vector src_pts; 46 | vector dst_pts; 47 | dst_pts.push_back(Point(width, height)); 48 | dst_pts.push_back(Point(0, height)); 49 | dst_pts.push_back(Point(0, 0)); 50 | dst_pts.push_back(Point(width, 0)); 51 | for (int i = 0; i < 4; i++) { 52 | printf("x=%.2f, y=%.2f\n", vertices[i].x, vertices[i].y); 53 | src_pts.push_back(vertices[i]); 54 | } 55 | 56 | // ͸�ӱ任 57 | Mat M = findHomography(src_pts, dst_pts); 58 | Mat result = Mat::zeros(Size(width, height), CV_8UC3); 59 | warpPerspective(src, result, M, result.size()); 60 | imshow("result", result); 61 | waitKey(0); 62 | return 0; 63 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_076/opencv_076.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | src = cv.imread("./test.jpg") 5 | cv.namedWindow("input", cv.WINDOW_AUTOSIZE) 6 | cv.imshow("input", src) 7 | 8 | # 图像二值化 9 | gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 10 | ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU) 11 | 12 | se = cv.getStructuringElement(cv.MORPH_RECT, (3, 3), (-1, -1)) 13 | binary = cv.morphologyEx(binary, cv.MORPH_OPEN, se) 14 | cv.imshow("binary", binary) 15 | cv.imwrite("./binary.png", binary) 16 | 17 | # 轮廓提取, 发现最大轮廓 18 | out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 19 | index = 0 20 | max = 0 21 | for c in range(len(contours)): 22 | area = cv.contourArea(contours[c]) 23 | if area > max: 24 | max = area 25 | index = c 26 | 27 | # 寻找最小外接矩形 28 | rect = cv.minAreaRect(contours[index]) 29 | print(rect[2]) 30 | print(rect[0]) 31 | # trick 32 | height, width = rect[1] 33 | print(rect[1]) 34 | box = cv.boxPoints(rect) 35 | src_pts = np.int0(box) 36 | print(src_pts) 37 | 38 | dst_pts = [] 39 | dst_pts.append([width,height]) 40 | dst_pts.append([0, height]) 41 | dst_pts.append([0, 0]) 42 | dst_pts.append([width, 0]) 43 | 44 | # 透视变换 45 | M, status = cv.findHomography(src_pts, np.array(dst_pts)) 46 | result = cv.warpPerspective(src, M, (np.int32(width), np.int32(height))) 47 | 48 | if height < width: 49 | result = cv.rotate(result, cv.ROTATE_90_CLOCKWISE) 50 | 51 | cv.imshow("result", result) 52 | cv.imwrite("./result.png", result) 53 | 54 | cv.waitKey(0) 55 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_076/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_076/result.png -------------------------------------------------------------------------------- /OpenCV_learn/code_076/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_076/test.jpg -------------------------------------------------------------------------------- /OpenCV_learn/code_077/opencv_077.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | void process_frame(Mat &image, int opts); 7 | int main(int argc, char** argv) { 8 | VideoCapture capture(0); 9 | 10 | if (!capture.isOpened()) { 11 | printf("could not open camera...\n"); 12 | return -1; 13 | } 14 | namedWindow("input", WINDOW_AUTOSIZE); 15 | 16 | int fps = capture.get(CAP_PROP_FPS); 17 | int width = capture.get(CAP_PROP_FRAME_WIDTH); 18 | int height = capture.get(CAP_PROP_FRAME_HEIGHT); 19 | int num_of_frames = capture.get(CAP_PROP_FRAME_COUNT); 20 | 21 | printf("frame width: %d, frame height: %d, FPS : %d \n", width, height, fps); 22 | 23 | Mat frame; 24 | int index = 0; 25 | while (true) { 26 | bool ret = capture.read(frame); 27 | if (!ret) break; 28 | imshow("input", frame); 29 | char c = waitKey(50); 30 | if (c >= 49) { 31 | index = c - 49; 32 | } 33 | process_frame(frame, index); 34 | imshow("result", frame); 35 | if (c == 27) { 36 | break; 37 | } 38 | } 39 | 40 | waitKey(0); 41 | return 0; 42 | } 43 | 44 | void process_frame(Mat &image, int opts) { 45 | Mat dst = image.clone(); 46 | if (opts == 0) { 47 | bitwise_not(image, dst); 48 | } 49 | if (opts == 1) { 50 | GaussianBlur(image, dst, Size(0, 0), 15); 51 | } 52 | if (opts == 2) { 53 | Canny(image, dst, 100, 200); 54 | } 55 | dst.copyTo(image); 56 | dst.release(); 57 | } 58 | -------------------------------------------------------------------------------- /OpenCV_learn/code_077/opencv_077.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | 3 | capture = cv.VideoCapture(0) 4 | height = capture.get(cv.CAP_PROP_FRAME_HEIGHT) 5 | width = capture.get(cv.CAP_PROP_FRAME_WIDTH) 6 | count = capture.get(cv.CAP_PROP_FRAME_COUNT) 7 | fps = capture.get(cv.CAP_PROP_FPS) 8 | print(height, width, count, fps) 9 | 10 | 11 | def process(image, opt=1): 12 | dst = None 13 | if opt == 0: 14 | dst = cv.bitwise_not(image) 15 | if opt == 1: 16 | dst = cv.GaussianBlur(image, (0, 0), 15) 17 | if opt == 2: 18 | dst = cv.Canny(image, 100, 200) 19 | return dst 20 | 21 | 22 | index = 0 23 | while(True): 24 | ret, frame = capture.read() 25 | if ret is True: 26 | cv.imshow("video-input", frame) 27 | c = cv.waitKey(50) 28 | if c >= 49: 29 | index = c -49 30 | result = process(frame, index) 31 | cv.imshow("result", result) 32 | print(c) 33 | if c == 27: #ESC 34 | break 35 | else: 36 | break 37 | cv.waitKey(0) 38 | cv.destroyAllWindows() 39 | 40 | -------------------------------------------------------------------------------- /OpenCV_learn/code_078/opencv_078.py: -------------------------------------------------------------------------------- 1 | import cv2 as cv 2 | import numpy as np 3 | 4 | # capture = cv.VideoCapture("D:/images/video/test.mp4") 5 | capture = cv.VideoCapture(0) 6 | height = capture.get(cv.CAP_PROP_FRAME_HEIGHT) 7 | width = capture.get(cv.CAP_PROP_FRAME_WIDTH) 8 | count = capture.get(cv.CAP_PROP_FRAME_COUNT) 9 | fps = capture.get(cv.CAP_PROP_FPS) 10 | print(height, width, count, fps) 11 | 12 | 13 | def process(image, opt=1): 14 | hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV) 15 | line = cv.getStructuringElement(cv.MORPH_RECT, (15, 15), (-1, -1)) 16 | mask = cv.inRange(hsv, (0, 43, 46), (10, 255, 255)) 17 | mask = cv.morphologyEx(mask, cv.MORPH_OPEN, line) 18 | 19 | # 轮廓提取, 发现最大轮廓 20 | out, contours, hierarchy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 21 | index = -1 22 | max = 0 23 | for c in range(len(contours)): 24 | area = cv.contourArea(contours[c]) 25 | if area > max: 26 | max = area 27 | index = c 28 | # 绘制 29 | if index >= 0: 30 | rect = cv.minAreaRect(contours[index]) 31 | cv.ellipse(image, rect, (0, 255, 0), 2, 8) 32 | cv.circle(image, (np.int32(rect[0][0]), np.int32(rect[0][1])), 2, (255, 0, 0), 2, 8, 0) 33 | return image 34 | 35 | 36 | while(True): 37 | ret, frame = capture.read() 38 | if ret is True: 39 | cv.imshow("video-input", frame) 40 | result = process(frame) 41 | cv.imshow("result", result) 42 | c = cv.waitKey(50) 43 | print(c) 44 | if c == 27: #ESC 45 | break 46 | else: 47 | break 48 | cv.waitKey(0) 49 | cv.destroyAllWindows() 50 | 51 | -------------------------------------------------------------------------------- /OpenCV_learn/code_079/opencv_079.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, char** argv) { 8 | 9 | VideoCapture capture(0); 10 | 11 | if (!capture.isOpened()) { 12 | printf("could not open camera...\n"); 13 | return -1; 14 | } 15 | namedWindow("input", WINDOW_AUTOSIZE); 16 | namedWindow("mask", WINDOW_AUTOSIZE); 17 | 18 | int fps = capture.get(CAP_PROP_FPS); 19 | int width = capture.get(CAP_PROP_FRAME_WIDTH); 20 | int height = capture.get(CAP_PROP_FRAME_HEIGHT); 21 | int num_of_frames = capture.get(CAP_PROP_FRAME_COUNT); 22 | printf("frame width: %d, frame height: %d, FPS : %d \n", width, height, fps); 23 | Ptr pMOG2 = createBackgroundSubtractorMOG2(500, 1000, false); 24 | Mat frame, mask, back_img; 25 | while (true) { 26 | bool ret = capture.read(frame); 27 | if (!ret) break; 28 | pMOG2->apply(frame, mask); 29 | pMOG2->getBackgroundImage(back_img); 30 | imshow("input", frame); 31 | imshow("mask", mask); 32 | imshow("back ground image", back_img); 33 | char c = waitKey(5); 34 | if (c == 27) { 35 | break; 36 | } 37 | } 38 | 39 | waitKey(0); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /OpenCV_learn/code_079/opencv_079.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 as cv 3 | 4 | cap = cv.VideoCapture(0) 5 | fgbg = cv.createBackgroundSubtractorMOG2(history=500, varThreshold=1000, detectShadows=False) 6 | while True: 7 | ret, frame = cap.read() 8 | fgmask = fgbg.apply(frame) 9 | background = fgbg.getBackgroundImage() 10 | cv.imshow('input', frame) 11 | cv.imshow('mask',fgmask) 12 | cv.imshow('background', background) 13 | k = cv.waitKey(10)&0xff 14 | if k == 27: 15 | break 16 | cap.release() 17 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/code_080/mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/code_080/mask.png -------------------------------------------------------------------------------- /OpenCV_learn/code_080/opencv_080.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | void process_frame(Mat &image, int opts); 7 | Ptr pMOG2 = createBackgroundSubtractorMOG2(500, 100, false); 8 | int main(int argc, char** argv) { 9 | VideoCapture capture(0); 10 | 11 | if (!capture.isOpened()) { 12 | printf("could not open camera...\n"); 13 | return -1; 14 | } 15 | namedWindow("input", WINDOW_AUTOSIZE); 16 | namedWindow("result", WINDOW_AUTOSIZE); 17 | 18 | int fps = capture.get(CAP_PROP_FPS); 19 | int width = capture.get(CAP_PROP_FRAME_WIDTH); 20 | int height = capture.get(CAP_PROP_FRAME_HEIGHT); 21 | int num_of_frames = capture.get(CAP_PROP_FRAME_COUNT); 22 | printf("frame width: %d, frame height: %d, FPS : %d \n", width, height, fps); 23 | 24 | Mat frame; 25 | while (true) { 26 | bool ret = capture.read(frame); 27 | imshow("input", frame); 28 | if (!ret) break; 29 | 30 | process_frame(frame, 0); 31 | imshow("result", frame); 32 | char c = waitKey(5); 33 | if (c == 27) { 34 | break; 35 | } 36 | } 37 | 38 | waitKey(0); 39 | return 0; 40 | } 41 | 42 | 43 | void process_frame(Mat &image, int opts) { 44 | Mat mask; 45 | pMOG2->apply(image, mask); 46 | // ����ṹԪ�� 47 | Mat se = getStructuringElement(MORPH_RECT, Size(1, 5), Point(-1, -1)); 48 | morphologyEx(mask, mask, MORPH_OPEN, se); 49 | 50 | // Ѱ��������� 51 | vector> contours; 52 | vector hierarchy; 53 | findContours(mask, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); 54 | for (size_t t = 0; t < contours.size(); t++) { 55 | double area = contourArea(contours[t]); 56 | if (area <100) { 57 | continue; 58 | } 59 | RotatedRect rect = minAreaRect(contours[t]); 60 | ellipse(image, rect, Scalar(0, 255, 0), 2, 8); 61 | circle(image, rect.center, 2, Scalar(255, 0, 0), 2, 8, 0); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /OpenCV_learn/code_080/opencv_080.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 as cv 3 | 4 | cap = cv.VideoCapture(0) 5 | fgbg = cv.createBackgroundSubtractorMOG2( 6 | history=500, varThreshold=100, detectShadows=False) 7 | 8 | 9 | def process(image, opt=1): 10 | mask = fgbg.apply(frame) 11 | line = cv.getStructuringElement(cv.MORPH_RECT, (1, 5), (-1, -1)) 12 | mask = cv.morphologyEx(mask, cv.MORPH_OPEN, line) 13 | cv.imshow("mask", mask) 14 | # 轮廓提取, 发现最大轮廓 15 | out, contours, hierarchy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 16 | for c in range(len(contours)): 17 | area = cv.contourArea(contours[c]) 18 | if area < 100: 19 | continue 20 | rect = cv.minAreaRect(contours[c]) 21 | cv.ellipse(image, rect, (0, 255, 0), 2, 8) 22 | cv.circle(image, (np.int32(rect[0][0]), np.int32(rect[0][1])), 2, (255, 0, 0), 2, 8, 0) 23 | return image, mask 24 | 25 | 26 | while True: 27 | ret, frame = cap.read() 28 | cv.imwrite("./input.png", frame) 29 | cv.imshow('input', frame) 30 | result, m_ = process(frame) 31 | cv.imshow('result', result) 32 | k = cv.waitKey(50)&0xff 33 | if k == 27: 34 | cv.imwrite("./result.png", result) 35 | cv.imwrite("./mask.png", m_) 36 | 37 | break 38 | cap.release() 39 | cv.destroyAllWindows() -------------------------------------------------------------------------------- /OpenCV_learn/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/OpenCV-/f3da4feb71c20d2e8bc426eb5a4e2e61a2fd4a75/OpenCV_learn/test.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenCV- 2 | OpenCV学习笔记 3 | --------------------------------------------------------------------------------