I am new to image processing. I am going to detect characters in the image using below code but the problem it does not detect characters that are very adjacent to the boundary.
import cv2
import numpy as np
import pytesseract
try:
import Image, ImageOps, ImageEnhance, imread
except ImportError:
from PIL import Image, ImageOps, ImageEnhance
image = cv2.imread(r"bmdc2.jpg")
image = cv2.blur(image, (3, 3))
ret, image = cv2.threshold(image, 90, 255, cv2.THRESH_BINARY)
image = cv2.dilate(image, np.ones((3, 1), np.uint8))
image = cv2.erode(image, np.ones((2, 2), np.uint8))
#image = cv2.dilate(image, np.ones((2, 2), np.uint8))
#enlarge the image to get rid off boundary letter
cv2.resize(image,(200,80))
cv2.imshow("1", image)
cv2.waitKey(0)
#convert to image in memroy
img = Image.fromarray(image)
text = pytesseract.image_to_string(img)
print(text)
cv2.imshow("1", np.array(image))
cv2.waitKey(0)
Image I am trying to read but it is detected as 8677 without beginning 6. is detected as 460973, as 25469.
Any better solution is welcome. Image sample can be found at https://www.bmdc.org.bd/search-doctor
Related
I am trying to count seeds in an image using cv2 thresholding. The test image is below:
When I run the below code to create a mask:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('S__14278933.jpg')
#img = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21)
mask = cv2.threshold(img[:, :, 0], 255, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
plt.imshow(mask)
I get the following mask:
But ideally it should give a small yellow dot at the centre. I have tried this with other images and it works just fine.
Can someone help?
The lighting in your image seems not uniform. Try using Adaptive Thresholding:
import cv2
import numpy as np
# image path
path = "D://opencvImages//"
fileName = "c6pBO.jpg"
# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)
# Convert the image to grayscale:
grayImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
# Get binary image via Adaptive Thresholding :
windowSize = 31
windowConstant = 40
binaryImage = cv2.adaptiveThreshold( grayImage, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, windowSize, windowConstant )
cv2.imshow("binaryImage", binaryImage)
cv2.waitKey(0)
You might want to apply an Area Filter after this, though, as dark portions on the image will yield noise.
Try it
img=cv2.imread('foto.jpg',0)
mask = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV )[1]
Is it possible to enlarge the upper margin of the image, keeping the background the same color? I have tried cv2 and pillow, but could not. It is to improve the scraping of image web pages with scrapy and tesseract in python
Input
Output
from PIL import Image
import pytesseract
import imutils
import cv2
import numpy as np
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract'
url = "https://www.yapo.cl/pg/0lu3quB3xJWOOJl5r7RBle3TME2kepf9ahuvYSfPYiA==.gif"
img = Image.open(urllib.request.urlopen(url))
img.save("image.png")
image = cv2.imread("image.png",0)
image = imutils.resize(image, width=400)
cv2.imshow('thresh', image)
cv2.waitKey()
kernel = np.ones((2,2), np.uint8)
cv2.imshow('thresh', image)
cv2.waitKey()
image = cv2.erode(image, kernel, iterations=1)
cv2.imshow('thresh', image)
cv2.waitKey()
image = cv2.dilate(image, kernel, iterations=1)
cv2.imshow('thresh', image)
cv2.waitKey()
image = cv2.GaussianBlur(image, (7,7), 0)
#result = 255 - thresh
image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
phone = pytesseract.image_to_string(image, config="-c tessedit_char_whitelist=0123456789+() --psm 6").strip()
print(phone)
PLease download the attatchment here and save it as /tmp/target.jpg.
You can see that there are 0244R in the jpg,i extract string with below python code:
from PIL import Image
import pytesseract
import cv2
filename = "/tmp/target.jpg"
image = cv2.imread(filename)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, threshold = cv2.threshold(gray,55, 255, cv2.THRESH_BINARY)
print(pytesseract.image_to_string(threshold))
What I get is
0244K
The right string is 0244R,how to make image more contrast, grayscale then get all characters exactly with with PIL and pytesseract?
Here is the webpage which generate the image :
http://www.crup.cn/ValidateCode/Index?t=0.14978241776661583
If you apply adaptive-thresholding and bitwise-not operations to the input image, the result will be:
Now if you remove the special characters like (dot, comma, etc..)
txt = pytesseract.image_to_string(bnt, config="--psm 6")
res = ''.join(i for i in txt if i.isalnum())
print(res)
Result will be:
O244R
Code:
import cv2
import pytesseract
img = cv2.imread("Aw6sN.jpg")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY_INV, 23, 100)
bnt = cv2.bitwise_not(thr)
txt = pytesseract.image_to_string(bnt, config="--psm 6")
res = ''.join(i for i in txt if i.isalnum())
print(res)
I am developing an application to read the numbers from an image using opencv in Python 3. I first converted the image to gray scale,then Apply dilation and erosion to remove some noise, then Apply threshold to get image with only black and white, then Write the image to local disk to do some ..., then apply tesseract to recognise the number for python.
I need to extract the numbers from the image. I am new to openCV. Does anybody know any other method to get the result??
I have share the image link bellow, i was trying to extract from that image. Thanks in advance
https://drive.google.com/file/d/141y-3okLPGP_STje14ukSqSHcgtwMdRO/view?usp=sharing
import cv2
import numpy as np
import pytesseract
from PIL import Image
from pytesseract import image_to_string
# Path of working folder on Disk
src_path = "/Users/sougata.a.roy/Desktop/Images/"
def get_string(img_path):
# Read image with opencv
img = cv2.imread(img_path)
# Convert to gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply dilation and erosion to remove some noise
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
# Write image after removed noise
cv2.imwrite(src_path + "removed_noise.jpg", img)
# Apply threshold to get image with only black and white
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 31, 2)
# Write the image after apply opencv to do some ...
cv2.imwrite(src_path + 'thres.jpg', img)
# Recognize text with tesseract for python
result = pytesseract.image_to_string(Image.open(src_path + "thres.jpg"), lang='eng')
return result
print('--- Start recognize text from image ---')
print(get_string(src_path + 'abcdefg195.jpg'))
print("------ Done -------")
365
I'm trying to implement line detection on python using raspberry pi 3 and picam
import picamera
import picamera.array
import time
import cv2
import numpy as np
#Initialize camera
camera = picamera.PiCamera()
camera.resolution = (640,480)
rawCapture = picamera.array.PiRGBArray(camera)
#Let camera warm up
time.sleep(0.1)
#Capture image
camera.capture(rawCapture, format="bgr")
img = rawCapture.array
#Convert to Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Blur image to reduce noise
blurred = cv2.GaussianBlur(gray, (9, 9), 0)
#Perform canny edge-detection
edged = cv2.Canny(blurred, 50, 150)
#Perform hough lines probalistic transform
lines = cv2.HoughLinesP(edged,1,np.pi/180,10,80,1)
#Draw lines on input image
if(lines != None):
for x1,y1,x2,y2 in lines[0]:
cv2.line(resized,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imshow("line detect test", img)
cv2.waitKey(0)
The following error is displayed:
Traceback (most recent call last): File "/home/pi/picam lane
detection.py", line 33, in
cv2.line(resize,(x1,y1),(x2,y2),(0,255,0),2) NameError: name 'resize' is not defined
Please help me figure out the issue.
I think the resized is not defined. Maybe you can change resized to img.
import picamera
import picamera.array
import time
import cv2
import numpy as np
#Initialize camera
camera = picamera.PiCamera()
camera.resolution = (640,480)
rawCapture = picamera.array.PiRGBArray(camera)
#Let camera warm up
time.sleep(0.1)
#Capture image
camera.capture(rawCapture, format="bgr")
img = rawCapture.array
#Convert to Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#Blur image to reduce noise
blurred = cv2.GaussianBlur(gray, (9, 9), 0)
#Perform canny edge-detection
edged = cv2.Canny(blurred, 50, 150)
#Perform hough lines probalistic transform
lines = cv2.HoughLinesP(edged,1,np.pi/180,10,80,1)
#Draw lines on input image
if(lines != None):
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imshow("line detect test", img)
cv2.waitKey(0)