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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| #include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std;
struct Block{ int row; int col; int height; int width; };
double variance(Mat img, vector<Block> blocks){ double ave_I = 0; int aero = 0; for (int i = 0; i < blocks.size(); i++) { for (int nrow = blocks[i].row; nrow < blocks[i].row+blocks[i].height; nrow++) { for (int ncol = blocks[i].col; ncol < blocks[i].col+blocks[i].width; ncol++) { ave_I += img.ptr<uchar>(nrow)[ncol]; } } aero += blocks[i].height * blocks[i].width; } ave_I /= double(aero); double v = 0; for (int i = 0; i < blocks.size(); i++) { for (int nrow = blocks[i].row; nrow < blocks[i].row+blocks[i].height; nrow++) { for (int ncol = blocks[i].col; ncol < blocks[i].col+blocks[i].width; ncol++) { v += (img.ptr<uchar>(nrow)[ncol] - ave_I)*(img.ptr<uchar>(nrow)[ncol] - ave_I); } } } v /= double(aero - 1); return v; }
void drawLines(Mat &img, vector<vector<Block>> homo_blocks){ for (int i = 0; i < homo_blocks.size(); i++) { Mat linePic = Mat::zeros(img.size(), CV_8UC1); for (int j = 0; j < homo_blocks[i].size(); j++) { for (int ncol = homo_blocks[i][j].col; ncol < homo_blocks[i][j].col+homo_blocks[i][j].width; ncol++) { int I = linePic.ptr<uchar>(homo_blocks[i][j].row)[ncol]; if (I == 0) { linePic.ptr<uchar>(homo_blocks[i][j].row)[ncol] = 255; } else if (I == 255) { linePic.ptr<uchar>(homo_blocks[i][j].row)[ncol] = 0; } I = linePic.ptr<uchar>(homo_blocks[i][j].row+homo_blocks[i][j].height)[ncol]; if (I == 0) { linePic.ptr<uchar>(homo_blocks[i][j].row+homo_blocks[i][j].height)[ncol] = 255; } else if (I == 255) { linePic.ptr<uchar>(homo_blocks[i][j].row+homo_blocks[i][j].height)[ncol] = 0; } } for (int nrow = homo_blocks[i][j].row; nrow < homo_blocks[i][j].row+homo_blocks[i][j].height; nrow++) { int I = linePic.ptr<uchar>(nrow)[homo_blocks[i][j].col]; if (I == 0) { linePic.ptr<uchar>(nrow)[homo_blocks[i][j].col] = 255; } else if (I == 255) { linePic.ptr<uchar>(nrow)[homo_blocks[i][j].col] = 0; } I = linePic.ptr<uchar>(nrow)[homo_blocks[i][j].col+homo_blocks[i][j].width]; if (I == 0) { linePic.ptr<uchar>(nrow)[homo_blocks[i][j].col+homo_blocks[i][j].width] = 255; } else if (I == 255) { linePic.ptr<uchar>(nrow)[homo_blocks[i][j].col+homo_blocks[i][j].width] = 0; } } } for (int nrow = 0; nrow < img.rows; nrow++) { for (int ncol = 0; ncol < img.cols; ncol++) { if (linePic.ptr<uchar>(nrow)[ncol] == 255) { img.ptr<uchar>(nrow)[ncol] = 255; } } } } }
void splitMerge (Mat img, Mat &result, double threshold){ Block init_block = { .row = 0, .col = 0, .height = img.rows, .width = img.cols }; vector<Block> blocks; vector<vector<Block>> homo_blocks; blocks.push_back(init_block); while (!blocks.empty()) { Block current_block = blocks.back(); blocks.pop_back(); if (current_block.height * current_block.width >= 8) { Block child_blocks[4] = { {.row = current_block.row, .col = current_block.col, .height = (current_block.height+1)/2, .width = (current_block.width+1)/2}, {.row = current_block.row, .col = current_block.col+(current_block.width+1)/2, .height = (current_block.height+1)/2, .width = current_block.width-(current_block.width+1)/2}, {.row = current_block.row+(current_block.height+1)/2, .col = current_block.col, .height = current_block.height-(current_block.height+1)/2, .width = (current_block.width+1)/2}, {.row = current_block.row+(current_block.height+1)/2, .col = current_block.col+(current_block.width+1)/2, .height = current_block.height-(current_block.height+1)/2, .width = current_block.width-(current_block.width+1)/2} }; for (int i = 0; i < 4; i++) { vector<Block> c_block = {child_blocks[i]}; if (variance(img, c_block) <= threshold) { if (homo_blocks.size() == 0) { homo_blocks.push_back(c_block); } else{ int n; double min_v = 0; int min_n = 0; for (n = 0; n < homo_blocks.size(); n++) { vector<Block> bk = homo_blocks[n]; bk.push_back(child_blocks[i]); if (n == 0) { min_v = variance(img, bk); } else{ if (variance(img, bk) < min_v) { min_v = variance(img, bk); min_n = n; } } } if (min_v <= threshold) { vector<Block> bk = homo_blocks[min_n]; bk.push_back(child_blocks[i]); homo_blocks[min_n] = bk; } else { homo_blocks.push_back(c_block); } } }else{ blocks.push_back(child_blocks[i]); } } } } drawLines(result, homo_blocks); }
int main() { Mat img,result; img = imread("img.jpg", IMREAD_GRAYSCALE); if (img.empty()) { printf("读取图像文件失败"); system("pause"); return -1; } result = img.clone(); double threshold = 200; splitMerge(img, result, threshold); imshow("img",img); imshow("result",result); imwrite("result.jpg", result); waitKey(); return 0; }
|