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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
| package org.lqs1848.cv.test;
import java.util.ArrayList; import java.util.List;
import org.bytedeco.javacpp.indexer.IntIndexer; import org.bytedeco.javacpp.indexer.UByteRawIndexer; import org.bytedeco.opencv.global.opencv_core; import org.bytedeco.opencv.global.opencv_imgcodecs; import org.bytedeco.opencv.global.opencv_imgproc; import org.bytedeco.opencv.opencv_core.Mat; import org.bytedeco.opencv.opencv_core.MatVector; import org.bytedeco.opencv.opencv_core.Point; import org.bytedeco.opencv.opencv_core.Rect; import org.bytedeco.opencv.opencv_core.Scalar; import org.bytedeco.opencv.opencv_core.Size;
public class OpenCV_LableCatch {
public static void main(String[] args) { Mat src = opencv_imgcodecs.imread("1.jpg", opencv_imgcodecs.IMREAD_GRAYSCALE);
int x = src.cols(); int y = src.rows();
if (x != y) { if (x < y) { int j = (y - x) / 2; src = src.apply(new Rect(0, j, x, x)); } else { int j = (x - y) / 2; src = src.apply(new Rect(j, 0, y, y)); } }
Mat gray = new Mat(); opencv_imgproc.resize(src, gray, new Size(3000, 3000));
Mat target = new Mat(); opencv_imgproc.threshold(gray, target, 0, 255, opencv_imgproc.THRESH_OTSU); opencv_imgcodecs.imwrite("E:\\cv\\ezh.jpg", target);
Mat kernel = new Mat(new Size(20, 20), opencv_core.CV_8UC1, new Scalar(255)); opencv_imgproc.morphologyEx(target, src, opencv_imgproc.MORPH_OPEN, kernel, new Point(-1, -1), 2, opencv_imgproc.MORPH_RECT, opencv_imgproc.morphologyDefaultBorderValue()); opencv_imgcodecs.imwrite("E:\\cv\\fspz.jpg", src);
opencv_core.bitwise_not(src, src); opencv_imgcodecs.imwrite("E:\\cv\\fs.jpg", src);
MatVector contours = new MatVector(); Mat hierarchy = new Mat(); opencv_imgproc.findContours(src, contours, hierarchy, opencv_imgproc.RETR_TREE, opencv_imgproc.CHAIN_APPROX_NONE);
Mat contours_img = new Mat(src.size(), opencv_core.CV_8U, new Scalar(255)); List<Rect> rects = new ArrayList<>(); IntIndexer hie = hierarchy.createIndexer(); for (int i = 0; i < contours.size(); i++) { if (hie.get(0, i, 2) == -1) { Mat contour = contours.get(i); double area = opencv_imgproc.contourArea(contour); double mins = 100; if (area > mins) { Rect rect = opencv_imgproc.boundingRect(contour); rects.add(rect); opencv_imgproc.drawContours(contours_img, contours, i, new Scalar(0), -1, opencv_imgproc.LINE_8, hierarchy, Integer.MAX_VALUE, new Point());
} } }
kernel = new Mat(new Size(7, 7), opencv_core.CV_8UC1, new Scalar(255)); int s1_id = 0, s2_id = 0, s1_jump = 0, s2_jump = 0; for (int i = 0; i < rects.size(); i++) { Rect rect = rects.get(i); Mat roi = target.apply(rect); if (roi.rows() <= roi.cols()) opencv_imgproc.resize(roi, roi, new Size(300, 100), 0, 0, opencv_imgproc.INTER_NEAREST); else opencv_imgproc.resize(roi, roi, new Size(100, 300), 0, 0, opencv_imgproc.INTER_NEAREST); opencv_imgproc.morphologyEx(roi, roi, opencv_imgproc.MORPH_GRADIENT, kernel, new Point(-1, -1), 1, opencv_imgproc.MORPH_RECT, opencv_imgproc.morphologyDefaultBorderValue()); int t_jump = stringJudge(roi);
if (t_jump >= s1_jump) { s2_id = s1_id; s2_jump = s1_jump; s1_id = i; s1_jump = t_jump; } else if (t_jump < s1_jump && t_jump >= s2_jump) { s2_id = i; s2_jump = t_jump; } }
if (rects.get(s1_id).width() < rects.get(s1_id).height() && rects.get(s1_id).tl().y() < rects.get(s2_id).tl().y()) { s1_id = s1_id + s2_id; s2_id = s1_id - s2_id; s1_id = s1_id - s2_id; }
Rect ns1 = rects.get(s1_id); if (isCentre(target, ns1)) ns1 = new Rect(ns1.x() - 10, ns1.y() - 10, ns1.width() + 20, ns1.height() + 20); Rect ns2 = rects.get(s2_id); if (isCentre(target, ns2)) ns2 = new Rect(ns2.x() - 10, ns2.y() - 10, ns2.width() + 20, ns2.height() + 20);
Mat dst1 = gray.apply(ns1); Mat dst2 = gray.apply(ns2);
if (dst1.cols() < dst1.rows()) { opencv_core.transpose(dst1, dst1); opencv_core.flip(dst1, dst1, 1); } if (dst2.cols() < dst2.rows()) { opencv_core.transpose(dst2, dst2); opencv_core.flip(dst2, dst2, 1); } opencv_imgproc.resize(dst1, dst1, new Size(300, 100), 0, 0, opencv_imgproc.INTER_NEAREST); opencv_imgcodecs.imwrite("E:\\cv\\r_111.jpg", dst1); dst1 = OpenCVUtils.adjustAngle(dst1); opencv_imgcodecs.imwrite("E:\\cv\\r_111_jz.jpg", dst1); opencv_imgproc.resize(dst2, dst2, new Size(300, 100), 0, 0, opencv_imgproc.INTER_NEAREST); opencv_imgcodecs.imwrite("E:\\cv\\r_222.jpg", dst2); dst2 = OpenCVUtils.adjustAngle(dst2); opencv_imgcodecs.imwrite("E:\\cv\\r_222_jz.jpg", dst2); }
public static boolean isCentre(Mat mat, Rect rect) { int x = Math.abs((mat.cols() / 2) - (mat.cols() - rect.x())); int y = Math.abs((mat.rows() / 2) - (mat.rows() - rect.y())); return x < mat.cols() / 3 && y < mat.rows() / 3; }
public static int stringJudge(Mat img) { UByteRawIndexer intIndexer = img.createIndexer(); int rows = img.rows(); int cols = img.cols(); int jump = 0; if (rows < cols) { for (int row = 0; row < rows; row++) { boolean wb_flag = false; boolean bw_flag = false; int t_jump = 0; for (int col = 0; col < cols; col++) { if (col + 1 < cols) { int now_point = intIndexer.get(row, col); int next_point = intIndexer.get(row, col + 1); if (now_point == 255 && next_point == 0) { wb_flag = true; } if (now_point == 0 && next_point == 255 && wb_flag) { bw_flag = true; } if (wb_flag && bw_flag) { ++t_jump; wb_flag = false; bw_flag = false; } } if (t_jump > jump) jump = t_jump; } } } else { for (int col = 0; col < cols; col++) { boolean wb_flag = false; boolean bw_flag = false; int t_jump = 0; for (int row = 0; row < rows; row++) { if (row + 1 < rows) { int now_point = intIndexer.get(row, col); int next_point = intIndexer.get(row + 1, col); if (now_point == 255 && next_point == 0) { wb_flag = true; } if (now_point == 0 && next_point == 255 && wb_flag) { bw_flag = true; } if (wb_flag && bw_flag) { ++t_jump; wb_flag = false; bw_flag = false; } } if (t_jump > jump) jump = t_jump; } } } return jump; } }
|