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
| #include "opencv2/opencv.hpp" #include <iostream> #include <string>
using namespace cv; using namespace std;
Mat image3; Mat target3;
void globalTwoValue(Mat src, int value) { target3 = Mat::zeros(src.size(), src.type());
for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++) { break; if (value > (int)src.at<uchar>(i, j)) target3.at<uchar>(i, j) = 0; else target3.at<uchar>(i, j) = 255; } } }
void localTwoValue(Mat src) { int countRow = src.rows / 8; int rowLeft = src.rows % 8;
int countCol = src.cols / 8; int colLeft = src.cols % 8;
target3 = Mat::zeros(src.size(), src.type());
for (int k = 0; k < countRow; k++) { for (int l = 0; l < countCol; l++) { int value = 0; for (int i = k * 8; i < (k + 1) * 8; i++) { for (int j = l * 8; j < (l + 1) * 8; j++) { value += (int)src.at<uchar>(i, j); } } value = value / 64; for (int i = k * 8; i < (k + 1) * 8; i++) { for (int j = l * 8; j < (l + 1) * 8; j++) { if ((int)src.at<uchar>(i, j) < value) target3.at<uchar>(i, j) = 0; else target3.at<uchar>(i, j) = 255; } } } }
if (rowLeft != 0) { for (int k = countRow; k < countRow + rowLeft; k++) { for (int l = 0; l < countCol; l++) { int value = 0; for (int i = countRow * 8; i < countRow * 8 + rowLeft; i++) { for (int j = l * 8; j < (l + 1) * 8; j++) { value += (int)src.at<uchar>(i, j); } } value = value / (8 * rowLeft); for (int i = countRow * 8; i < countRow * 8 + rowLeft; i++) { for (int j = l * 8; j < (l + 1) * 8; j++) { if ((int)src.at<uchar>(i, j) < value) target3.at<uchar>(i, j) = 0; else target3.at<uchar>(i, j) = 255; } } } } } if (colLeft != 0) { for (int k = 0; k < countRow; k++) { for (int l = countCol; l < countCol + colLeft; l++) { int value = 0; for (int i = k * 8; i < (k + 1) * 8; i++) { for (int j = countCol * 8; j < countCol * 8 + colLeft; j++) { value += (int)src.at<uchar>(i, j); } } value = value / (8 * colLeft); for (int i = k * 8; i < (k + 1) * 8; i++) { for (int j = countCol * 8; j < countCol * 8 + colLeft; j++) { if ((int)src.at<uchar>(i, j) < value) target3.at<uchar>(i, j) = 0; else target3.at<uchar>(i, j) = 255; } } } } } if (rowLeft != 0 && colLeft != 0) { int value = 0; for (int i = 8 * countRow; i < src.rows; i++) { for (int j = 8 * countCol; j < src.cols; j++) { value += (int)src.at<uchar>(i, j); } } value = value / (rowLeft * colLeft); for (int i = 8 * countRow; i < src.rows; i++) { for (int j = 8 * countCol; j < src.cols; j++) { if ((int)src.at<uchar>(i, j) < value) target3.at<uchar>(i, j) = 0; else target3.at<uchar>(i, j) = 255; } } } }
int main() { image3 = imread("1.jpg", 0);
if (image3.empty()) { printf("could not load pic!\n"); return -1; }
imshow("image3", image3);
localTwoValue(image3);
imshow("target3", target3);
waitKey(0);
return 0; }
|