The difference between the conversion functions OPENV BGR2GRAY and Pillow

I am trying to display an image that includes both numbers and characters using the Tesseract library with opencv and C ++. Before calling the tesseract library, I used an image with opencv for grayscale

cvtColor(roiImg,roiImg,CV_BGR2GRAY);

it Gray scale image i received with python

The OCR results for this image were not 100% accurate.

Then the same image was tested using a library of python pillows. The original image was grayscale using the following method.

gray = image.convert('L')

it Gray Scale image i received with pillow library

The last mentioned gray scaled image gave 100% accurate results.

As soon as I looked over the Internet, it was mentioned that both opencv BGR2Gray and the cushion method img.convert use the same brightness conversion algorithm.

What is the reason for two different OCR results?

Advance

+4
1

Pillow 38- .

, , :

  • OpenCV:

    cv::Mat img(2, 1, CV_8UC3), img_gray;
    img.at<cv::Vec3b>(0, 0) = cv::Vec3b(248, 249, 249); //BGR
    img.at<cv::Vec3b>(1, 0) = cv::Vec3b(249, 248, 248); //BGR
    
    cv::cvtColor(img, img_gray, cv::COLOR_BGR2GRAY);
    std::cout << "img:\n" << img << std::endl;
    std::cout << "img_gray:\n" << img_gray << std::endl;
    
    float val1 = 249*0.299f + 249*0.587f + 248*0.114f; //RGB
    float val2 = 248*0.299f + 248*0.587f + 249*0.114f; //RGB
    std::cout << "val1=" << val1 << std::endl;
    std::cout << "val2=" << val2 << std::endl;
    

IMG:

[248, 249, 249;

249, 248, 248]

img_gray:

[249;

248]

1 = 248,886

2 = 248,114

  • Python:

    rgbArray = np.zeros((2,1,3), 'uint8')
    rgbArray[0,0,0] = 249 #R
    rgbArray[0,0,1] = 249 #G
    rgbArray[0,0,2] = 248 #B
    rgbArray[1,0,0] = 248 #R
    rgbArray[1,0,1] = 248 #G
    rgbArray[1,0,2] = 249 #B
    
    img = Image.fromarray(rgbArray)
    imgGray = img.convert('L')
    
    print("rgbArray:\n", rgbArray)
    print("imgGray:\n", np.asarray(imgGray))
    print("np.asarray(imgGray).dtype: ", np.asarray(imgGray).dtype)
    

rgbArray:

[[[249 249 248]]

[[248 248 249]]]

imgGray:

[[248]

[248]]

np.asarray(imgGray).dtype: uint8

0

Source: https://habr.com/ru/post/1668588/


All Articles