Thinnig of a binary image: python - python

I was trying to skeletonize an image. Using this code, to an extent the image came out as I wanted. But still there are some issues in the image. I can find some gaps in between the lines. Is there any better alogorithm to get rid of those gaps.I have attached the input and the output of the given code.
import numpy as np
import cv2
img = cv2.imread("e_5.jpg",0)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)
ret,img = cv2.threshold(img,127,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
img = 255 - img
img = cv2.dilate(img, element, iterations=3)
done = False
while( not done):
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
zeros = size - cv2.countNonZero(img)
if zeros==size:
done = True

Related

how to extract numbers from captcha image in python?

I want to extract numbers from captcha image, so I tried this code from this answer this answer:
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
import cv2
file = 'sample.jpg'
img = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, None, fx=10, fy=10, interpolation=cv2.INTER_LINEAR)
img = cv2.medianBlur(img, 9)
th, img = cv2.threshold(img, 185, 255, cv2.THRESH_BINARY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4,8))
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imwrite("sample2.jpg", img)
file = 'sample2.jpg'
text = pytesseract.image_to_string(file)
print(''.join(x for x in text if x.isdigit()))
and it worked fine for this image: outPut: 436359 But, when I tried it on this image: It gave me nothing, outPut: .
How can I modify my code to get the numbers as a string from the second image?
EDIT:
I tried Matt's answer and it worked just fine for the image above. but it doesn't recognise numbers like (8,1) in image A, and number (7) in image B
image A
image B
How to fix that?
Often, getting OCR just right on an image like this has to do with the order and parameters of the transformations. For example, in the following code snippet, I first convert to grayscale, then erode the pixels, then dilate, then erode again. I use threshold to convert to binary (just blacks and whites) and then dilate and erode one more time. This for me produces the correct value of 859917 and should be reproducible.
import cv2
import numpy as np
import pytesseract
file = 'sample2.jpg'
img = cv2.imread(file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ekernel = np.ones((1,2),np.uint8)
eroded = cv2.erode(gray, ekernel, iterations = 1)
dkernel = np.ones((2,3),np.uint8)
dilated_once = cv2.dilate(eroded, dkernel, iterations = 1)
ekernel = np.ones((2,2),np.uint8)
dilated_twice = cv2.erode(dilated_once, ekernel, iterations = 1)
th, threshed = cv2.threshold(dilated_twice, 200, 255, cv2.THRESH_BINARY)
dkernel = np.ones((2,2),np.uint8)
threshed_dilated = cv2.dilate(threshed, dkernel, iterations = 1)
ekernel = np.ones((2,2),np.uint8)
threshed_eroded = cv2.erode(threshed_dilated, ekernel, iterations = 1)
text = pytesseract.image_to_string(threshed_eroded)
print(''.join(x for x in text if x.isdigit()))

How to save multiple images in one folder by using open cv?

Here is the code that i'm trying to modify. To read and save all the images at once in one folder, however I got error when I tried to save it
import cv2
import glob
import numpy as np
#empty lists
image_list=[]
images = []
for img in glob.glob(r"C:\Users\user\Documents\Dataset\Test\Abnormal_Resize\Abnormal_Crop\*.png"):
n= cv2.imread(img)
images.append(n)
print (img)
#limit list to 236 elements
image_list = image_list[:100]
#Resizing the image for compatibility
for img in image_list:
image = cv2.resize(image, (500, 600))
#The initial processing of the image
#image = cv2.medianBlur(image, 3)
image_bw = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#The declaration of CLAHE
#clipLimit -> Threshold for contrast limiting
clahe = cv2.createCLAHE(clipLimit = 5)
final_img = clahe.apply(image_bw)
cv2.imwrite(path+r"C:\Users\user\Documents\Dataset\Test\Abnormal_Resize\Abnormal_Crop\Abnormal_Cntrst\contrast_"+str(i)+".png", final_img)
It seems like there are multiple issues with this code.
i is not defined.
images list has all the images appended and while processing you are making use of the empty list variable image_list
You are probably looking for a solution like this.
import cv2
import glob
import numpy as np
input_path = r"<your_input_folder_path>/*.png"
# make sure below folder already exists
out_path = '<your_output_folder_path>/'
image_paths = list(glob.glob(input_path))
for i, img in enumerate(image_paths):
image = cv2.imread(img)
image = cv2.resize(image, (500, 600))
#The initial processing of the image
#image = cv2.medianBlur(image, 3)
image_bw = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#The declaration of CLAHE
#clipLimit -> Threshold for contrast limiting
clahe = cv2.createCLAHE(clipLimit = 5)
final_img = clahe.apply(image_bw)
cv2.imwrite(out_path + f'{str(i)}.png', final_img)

Make background of image white in python with canny edge detection

I would like to change the background of this image to a white background in python. I tried canny edge detection, but it has a hard time finding the edges at the top of the product as you can see in the second picture. I tried different thresholds, but it would result in more background being not white. This is probably due to the fact that the top of the product in the image has almost the same color as the background.
Is there a method to detect a small difference like that? I also tried it with a green screen behind the product, but because of the reflective state of the product, the product turns green.
Here is my code:
import cv2
import numpy as np
from skimage import filters
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
plt.rcParams['figure.dpi'] = 200
#== Parameters =======================================================================
BLUR = 15
CANNY_THRESH_1 = 10
CANNY_THRESH_2 = 255
MASK_DILATE_ITER = 10
MASK_ERODE_ITER = 10
MASK_COLOR = (1.0,1.0,1.0) # In BGR format
#== Processing =======================================================================
mypath = "./images"
images = [f for f in listdir(mypath) if isfile(join(mypath, f))]
#-- Read image -----------------------------------------------------------------------
for image in images:
img_loc = mypath + "/" + image
img = cv2.imread(img_loc)
# threshold
img_thresh = img
thresh = 180
img_thresh[ img_thresh >= thresh ] = 255
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#-- Edge detection -------------------------------------------------------------------
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
edges = cv2.dilate(edges, None)
edges = cv2.erode(edges, None)
#-- Find contours in edges, sort by area ---------------------------------------------
contour_info = []
contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
for c in contours:
contour_info.append((
c,
cv2.isContourConvex(c),
cv2.contourArea(c),
))
contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
max_contour = contour_info[0]
mask = np.zeros(edges.shape)
cv2.fillConvexPoly(mask, max_contour[0], (255))
#-- Smooth mask, then blur it --------------------------------------------------------
mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
mask_stack = np.dstack([mask]*3) # Create 3-channel alpha mask
#-- Blend masked img into MASK_COLOR background --------------------------------------
mask_stack = mask_stack.astype('float32') / 255.0 # Use float matrices,
img = img.astype('float32') / 255.0 # for easy blending
masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR) # Blend
masked = (masked * 255).astype('uint8') # Convert back to 8-bit
result_dir = "./results/" + image
cv2.imwrite(result_dir, masked) # Save

Middle line between 2 contour lines in opencv python?

I have an image
After my code runs,
The new image is
I need to find the line between them like this
How do I do?
My code
import numpy as np
import cv2
import cv2 as cv
ima = cv2.imread('track1.pNg')
imgray = cv2.cvtColor(ima,cv2.COLOR_BGR2GRAY)
im = cv2.cvtColor(ima,cv2.COLOR_BGR2GRAY)
imm = cv2.inRange(im,(0),(49))
kernel = np.ones((5,5),np.uint8)
gradient = cv2.morphologyEx(imm, cv2.MORPH_GRADIENT, kernel)
il = cv2.dilate(gradient, kernel, iterations=7)
ol = cv2.erode(il, kernel, iterations=7)
contours,hei = cv2.findContours(ol,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
img = cv2.drawContours(ima, contours, -1, (200,255,0), 3)
cv2.imshow('window',ima)
How can i achieve this?
This answer explains how to find a line that runs between two sides of a shape. The center can be found by iteratively eroding the image.
This is the result:
This is the code I used:
import cv2
import numpy as np
img = 255-cv2.imread('/home/stephen/Desktop/PITqe.png',0)
kernel = np.ones((20,20), np.uint8)
img = cv2.erode(img, kernel, iterations=2)
size = np.size(img)
skel = np.zeros(img.shape,np.uint8)
ret,img = cv2.threshold(img,127,255,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
done = False
while( not done):
eroded = cv2.erode(img,element)
temp = cv2.dilate(eroded,element)
temp = cv2.subtract(img,temp)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()
zeros = size - cv2.countNonZero(img)
cv2.imshow('img', img)
cv2.waitKey(100)
if zeros==size:
done = True
cv2.imshow("img",skel)
cv2.waitKey(0)
cv2.destroyAllWindows()
Here is another way to skeletonize in OpenCV (without explicitly iterating) by using distance transform and top hat morphology.
Input:
import cv2
import numpy as np
# read image and invert so blob is white on black background
img = 255-cv2.imread('tall_blob.png',0)
# do some eroding of img, but not too much
kernel = np.ones((20,20), np.uint8)
img = cv2.erode(img, kernel, iterations=2)
# threshold img
ret, thresh = cv2.threshold(img,127,255,0)
# do distance transform
dist = cv2.distanceTransform(thresh, distanceType=cv2.DIST_L2, maskSize=5)
# set up cross for tophat skeletonization
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
skeleton = cv2.morphologyEx(dist, cv2.MORPH_TOPHAT, kernel)
# threshold skeleton
ret, skeleton = cv2.threshold(skeleton,0,255,0)
# display skeleton
cv2.imshow("skeleton",skeleton)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save results
cv2.imwrite('tall_blob_skeleton.png', skeleton)

Extract pixel coordinates and paste on new image python

This code is returning pixels coordinates which have red color now i want to extract and paste those pixels on new image. how do i paste pixel coordinates? Please ask if question is not clear.
import cv2
import numpy as np
filename = "oOHc6.png"
img = cv2.imread(filename, 1)
hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv_lower=np.uint8([0, 200, 210])
hsv_upper=np.uint8([180, 250, 250])
mask= cv2.inRange(hsv, hsv_lower, hsv_upper)
#display mask
res = cv2.bitwise_and(img,img,mask = mask)
res_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
ys,xs = np.where(res_gray>0)
pts = [(x,y) for x,y in zip(xs,ys)]
empty = np.zeros_like(img)
mask_c = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
imaskc = mask_c>0
empty[imaskc] = img[imaskc]
#empty.save('C:/Python27/cclabel/images/NewImage'+'.png','png')
cv2.imwrite("new.png", empty)
I do cv2.inRange() in HSV-space to get the mask for the red region:
Then use the mask-operation(such as cv2.bitwise_and()/np.where()/ slices) to "paste" to another image.
To get the coords, you can also use the np.where() like that.
# 使用 cv2.bitwise_and 掩模操作,然后使用 np.where 获取坐标
res = cv2.bitwise_and(img,img,mask = mask)
res_gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
ys,xs = np.where(res_gray>0)
pts = [(x,y) for x,y in zip(xs,ys)]
To "copy-paste" into another same size image:
## 复制-粘贴到其他空白的地方
empty = np.zeros_like(img)
mask_c = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
imaskc = mask_c>0
empty[imaskc] = img[imaskc]

Categories