1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv;
struct Kernel{ Mat B; int position[2]; };
Kernel getRectKernel(int* size, int* position) { Kernel kernel; kernel.B = Mat::ones(size[0], size[1], CV_8U); for (int nrow = 0; nrow < kernel.B.rows; nrow++) { for (int ncol = 0; ncol < kernel.B.cols; ncol++) { kernel.B.ptr<uchar>(nrow)[ncol] = 255; } } kernel.position[0] = position[0]; kernel.position[1] = position[1]; return kernel; }
void getErosion(Mat img, Mat &dst, Kernel kernel){ dst = img.clone(); Mat B = kernel.B; int r_pos = kernel.position[0]; int c_pos = kernel.position[1]; for (int nrow = r_pos; nrow < img.rows-r_pos; nrow++) { for (int ncol = c_pos; ncol < img.cols-c_pos; ncol++) { bool bingo = true; if (img.ptr<uchar>(nrow)[ncol] == 255) { for (int i = 0; i < B.rows; i++) { for (int j = 0; j < B.cols; j++) { if (B.ptr<uchar>(i)[j] == 255) { if (img.ptr<uchar>(i+nrow-r_pos)[j+ncol-c_pos] != 255) { bingo = false; break; } } } } } if (!bingo) { dst.ptr<uchar>(nrow)[ncol] = 0; } } } }
void getDilation(Mat img, Mat &dst, Kernel kernel) { dst = img.clone(); Mat B = kernel.B; Mat B_x = Mat::zeros(B.cols, B.rows, CV_8U); for (int nrow = 0; nrow < B.rows; nrow++) { for (int ncol = 0; ncol < B.cols; ncol++) { B_x.ptr<uchar>(ncol)[nrow] = B.ptr<uchar>(nrow)[ncol]; } } int r_pos = kernel.position[1]; int c_pos = kernel.position[0]; for (int nrow = 0; nrow < img.rows; nrow++) { for (int ncol = 0; ncol < img.cols; ncol++) { bool bingo = false;
for (int i = 0; i < B_x.rows; i++) { for (int j = 0; j < B_x.cols; j++) { int row = nrow - r_pos + i; int col = ncol - c_pos + j; if (B_x.ptr<uchar>(i)[j] == 255) { if (row >= 0 && row < img.rows && col >= 0 && col < img.cols) { if (img.ptr<uchar>(row)[col] == 255) { bingo = true; break; } } } } if (bingo) { break; } } if (bingo) { dst.ptr<uchar>(nrow)[ncol] = 255; } else { dst.ptr<uchar>(nrow)[ncol] = 0; } } } }
int main() { Mat img3, erosion_dst, opening_dst; img3 = imread("img3.png", IMREAD_GRAYSCALE); if (img3.empty()) { printf("读取图像文件失败"); system("pause"); return -1; } Mat img3_twoColor; threshold(img3, img3_twoColor, 0, 255, THRESH_OTSU); int size[2] = {4, 4}; int position[2] = {0, 0}; Kernel kernel = getRectKernel(size, position); getErosion(img3_twoColor, erosion_dst, kernel); getDilation(erosion_dst, opening_dst, kernel); imshow("img3",img3); imshow("erosion_dst", erosion_dst); imshow("opening_dst", opening_dst); imwrite("opening_dst.jpg", opening_dst); waitKey(); return 0; }
|