OpenC在图像处理中保持字符排序

2024-06-28 20:57:47 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,我尝试预处理图像,并使用findContours将每个字符提取为图像。在

    Mat inImage = Imgcodecs.imread("CaptureMultiLines.jpg", Imgcodecs.CV_LOAD_IMAGE_COLOR);
    Mat destination = new Mat(inImage.rows(), inImage.cols(), inImage.type());
    ImageProcUtils.showImage("initial", inImage);

   // convert to grayscale
    Imgproc.cvtColor(inImage, destination, Imgproc.COLOR_BGR2GRAY);
    ImageProcUtils.showImage("grayscaleimage", destination);

    Mat binImg = new Mat(inImage.rows(), inImage.cols(), inImage.type());

    // binarize the image
    double thresh = Imgproc.threshold(destination, binImg, 127, 255, Imgproc.THRESH_BINARY_INV);
    ImageProcUtils.showImage("Binary Image", binImg);

   // dilation
    Mat dilMat = new Mat(inImage.rows(), inImage.cols(), inImage.type());
    Mat kernel = Mat.ones(2,1, CvType.CV_8U); // able to extract character
    Imgproc.dilate(binImg, dilMat, kernel);

    ImageProcUtils.showImage("Dilated Image", dilMat);

    // find contours
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
    Mat hierarchy = new Mat();
    Imgproc.findContours(dilMat.clone(), contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    System.out.println();

    ImageProcUtils.showImage("Contours", dilMat);

    CharacterSegmentation inst = new CharacterSegmentation();        

    for (int j = 0; j < contours.size(); j++) {
         Imgproc.drawContours(dilMat, contours, j, new Scalar(255,0,0));
    }

    Mat drawing = Mat.zeros( dilMat.size(), CvType.CV_8UC3 );
    Point centroid = inst.massCenterMatOfPoint2f(submat);

    for( int i = 0; i< contours.size(); i++ )
    {

        Rect box = Imgproc.boundingRect(contours.get(i));

        Mat submat = dilMat.submat(box);

        int[] flattenedArr = inst.flattenAnImage(submat);
        ImageProcUtils.printArray("Contour.."+i, flattenedArr);

        Imgcodecs.imwrite("character-Line"+ i +".jpg", submat);
    }

下面是我正在使用的图像

enter image description here

我可以成功地提取字符,但不能保持顺序。我想根据每个字符在图像中的出现来命名它。例如,F应该命名为Character-0-0,这意味着它出现在第一行和第一列。类似地,BCharacter-2-7

我知道有很多类似的线索

Trying to segment characters and save it in order to image files. But contours are being drawn in a different order?OpenCV findContours are not in orderHow can I sort contours from left to right and top to bottom?

但是,我还是不知道如何实现这一点。在

我也尝试了下面的Python代码,但还是没有成功

^{pr2}$

Tags: to图像new字符destinationmatcontoursfindcontours
2条回答

试试这个:

import cv2
import numpy as np
image = cv2.imread("D:\\Users\\Downloads\\CaptureMultiLines.jpg")
cv2.imshow('orig',image)
# image = cv2.resize(image_original,None,fx=4, fy=4, interpolation = cv2.INTER_CUBIC)

#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# original_resized = cv2.resize(gray, (0,0), fx=.2, fy=.2)
cv2.imshow('gray',gray)
cv2.waitKey(0)

#Remove Salt and pepper noise
saltpep = cv2.fastNlMeansDenoising(gray,None,9,13)
# original_resized = cv2.resize(saltpep, (0,0), fx=.2, fy=.2)
cv2.imshow('Grayscale',saltpep)
cv2.waitKey(0)

#blur
blured = cv2.blur(saltpep,(3,3))
# original_resized = cv2.resize(blured, (0,0), fx=.2, fy=.2)
cv2.imshow('blured',blured)
cv2.waitKey(0)

#binary
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
# original_resized = cv2.resize(thresh, (0,0), fx=.2, fy=.2)
cv2.imshow('Threshold',thresh)
cv2.waitKey(0)

#dilation
kernel = np.ones((5,100), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
# original_resized = cv2.resize(img_dilation, (0,0), fx=.2, fy=.2)
cv2.imshow('dilated',img_dilation)
cv2.waitKey(0)

#find contours
im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[1])

for i, ctr in enumerate(sorted_ctrs):

    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y+h, x:x+w]

# #   show ROI
    cv2.imshow('segment no:' +str(i),roi)
    cv2.waitKey(0)


    im = cv2.resize(roi,None,fx=4, fy=4, interpolation = cv2.INTER_CUBIC)
    ret_1,thresh_1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY_INV)
    # original_resized = cv2.resize(thresh, (0,0), fx=.2, fy=.2)
    cv2.imshow('Threshold_1',thresh_1)
    cv2.waitKey(0)
    thresh_1=cv2.cvtColor(thresh_1, cv2.COLOR_BGR2GRAY);

    #find contours
    im,ctrs_1, hier = cv2.findContours(thresh_1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    #sort contours
    sorted_ctrs_1 = sorted(ctrs_1, key=lambda ctr: cv2.boundingRect(ctr)[0])

    for j, ctr_1 in enumerate(sorted_ctrs_1):

        # Get bounding box
        x_1, y_1, w_1, h_1 = cv2.boundingRect(ctr_1)

        # Getting ROI
        roi_1 = thresh_1[y_1:y_1+h_1, x_1:x_1+w_1]

        # #   show ROI
        cv2.imshow('Line no: ' + str(i) + "Column no : " +str(j),roi_1)
        cv2.waitKey(0)


# original_resized = cv2.resize(image, (0,0), fx=.2, fy=.2)
# cv2.imshow('marked areas',original_resized)
cv2.imshow('marked areas',image)
cv2.waitKey(0)

这是我用来做同样事情的代码,但是使用python:

sorted_contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0] + cv2.boundingRect(ctr)[1] * image.shape[1] )

希望这会有帮助。这将根据字符的x和y坐标对轮廓进行排序。在

相关问题 更多 >