Search code examples
pythonopencvimage-processingpython-tesseract

engraved imprint detection on pills using python


enter image description here

enter image description here

import cv2
import pytesseract
from PIL import Image
import numpy as np
# import bm3d
img = cv2.imread('1_2_2.png')
# img = cv2.medianBlur(img, 5)
img = cv2.GaussianBlur(img,(13,13),0)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# gray = cv2.medianBlur(, 5)
# cv2.imshow("img", gray)
# gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | 
cv2.THRESH_OTSU) 
[1]
v = np.median(gray)
sigma = 0.33
#---- apply automatic Canny edge detection using the computed median-- 
--
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
gray = cv2.Canny(img,lower,upper)
# ret,gray = cv2.threshold(gray,110,255,cv2.THRESH_BINARY)
kernel = np.ones((4,4),np.uint8)
gray = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel)
gray = cv2.dilate(gray,kernel,iterations = 1)
# gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)\
# gray = cv2.erode(gray,kernel,iterations = 1)
gray = cv2.bitwise_not(gray)
cv2.imshow("threshold", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
# gray = cv2.medianBlur(gray, 3)
text = pytesseract.image_to_string(gray)
print(text)

enter image description here

I am trying some image processing as mentioned in the code but not able to get the one which pytesseract can detect.
please help is there any work done to detect the engravings

seen that stack overflow link but did't get the proper idea


Solution

  • Please Note: This is just a starter's code. You can play around with it, and also it involves a lot of threshold values, which you need to experiment with. Of course this is not the best code, but you can use it as a starting point.

    I will briefly outline the steps below, and provide the python code after that along with the output that it generates at each step.

    • Load image in Grayscale
    • Perform Adaptive Threshold with a large kernel size. It is important to perform Adaptive Threshold, rather than some Global Threshold, as it takes neighboring intensity into account, which plays an important role in the example image provided by you.
    • Perform Median Blur to get rid of salt and pepper noise.
    • Find the Connected Components with considerable area, and remove small island noise from the final image.
    • Plot the final contours into the output image.

    The python code to achieve this is provided below:

    import cv2
    import numpy as np
    
    image = cv2.imread('test.png')
    output = np.zeros((image.shape[0],image.shape[1],3), np.uint8)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    threshold = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C  , cv2.THRESH_BINARY, 11, 1)
    
    median = cv2.medianBlur(threshold, 11)
    median = cv2.bitwise_not(median)
    
    im2, contours, hierarchy = cv2.findContours(median,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    
    saved_cont = []
    thresh = 100
    
    for contour in contours:
        if cv2.contourArea(contour) > thresh:
            print(cv2.contourArea(contour))
            saved_cont.append(contour)
    
    cv2.drawContours(output, saved_cont,-1,(255,255,255),1)
    
    cv2.imshow('original', gray)
    cv2.imshow('threshold', threshold)
    cv2.imshow('median', median)
    cv2.imshow('contour', output)
    
    cv2.imwrite("threshold.png", threshold)
    cv2.imwrite("median.png", median)
    cv2.imwrite("output.png", output)
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Original Image:

    enter image description here

    Thresholded Image:

    enter image description here

    Median Blurred Image:

    enter image description here

    Final Output:

    enter image description here

    Some other morphological operations that you may want to experiment with are Dilation, Erosion, Opening and Closing operations. The documentation can be found over here.