Is there a better way to separate the writing from the background? - python

I am working on a project where I should apply and OCR on some documents.
The first step is to threshold the image and let only the writing (whiten the background).
Example of an input image: (For the GDPR and privacy reasons, this image is from the Internet)
Here is my code:
import cv2
import numpy as np
image = cv2.imread('b.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
h = image.shape[0]
w = image.shape[1]
for y in range(0, h):
for x in range(0, w):
if image[y, x] >= 120:
image[y, x] = 255
else:
image[y, x] = 0
cv2.imwrite('output.jpg', image)
Here is the result that I got:
When I applied pytesseract to the output image, the results were not satisfying (I know that an OCR is not perfect). Although I tried to adjust the threshold value (in this code it is equal to 120), the result was not as clear as I wanted.
Is there a way to make a better threshold in order to only keep the writing in black and whiten the rest?

After digging deep in StackOverflow questions, I found this answer which is about removing watermark using opencv.
I adapted the code to my needs and this is what I got:
import numpy as np
import cv2
image = cv2.imread('a.png')
img = image.copy()
alpha =2.75
beta = -160.0
denoised = alpha * img + beta
denoised = np.clip(denoised, 0, 255).astype(np.uint8)
#denoised = cv2.fastNlMeansDenoising(denoised, None, 31, 7, 21)
img = cv2.cvtColor(denoised, cv2.COLOR_BGR2GRAY)
h = img.shape[0]
w = img.shape[1]
for y in range(0, h):
for x in range(0, w):
if img[y, x] >= 220:
img[y, x] = 255
else:
img[y, x] = 0
cv2.imwrite('outpu.jpg', img)
Here is the output image:
The good thing about this code is that it gives good results not only with this image, but also with all the images that I tested.
I hope it helps anyone who had the same problem.

You can use adaptive thresholding. From documentation :
In this, the algorithm calculate the threshold for a small regions of the image. So we get different thresholds for different regions of the same image and it gives us better results for images with varying illumination.
import numpy as np
import cv2
image = cv2.imread('b.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.medianBlur(image ,5)
th1 = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
cv2.THRESH_BINARY,11,2)
th2 = cv2.adaptiveThreshold(image,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)
cv2.imwrite('output1.jpg', th1 )
cv2.imwrite('output2.jpg', th2 )

Related

Remove background of Plant Village dataset

I’m trying to remove the background from a dataset of leaves, during my research I found out this paper that describes the steps to extract the plant with respect to the background as below.
However, I cannot understand how to check if the pixel(x,y) is at the edge of Id.
So far, I have implement the previous steps:
import numpy as np
import cv2
import skimage.morphology as morph
image = cv2.imread(filename)
height, width, _ = image.shape
Ihsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
Is = Ihsv[:,:,2]
#Apply Spacial Gaussian Filter
trunc_val = 2
sigma_val = 2
k_size = int(sigma_val * trunc_val)
Ifg = cv2.GaussianBlur(Is, (k_size,k_size), sigma_val)
A = np.zeros((height, width), dtype=np.uint8)
for h in range(0, height):
for w in range(0, width):
if Ifg[h, w] > 0.23:
A[h, w] = 1
#Apply Morphological Operations
kernelD2 = morph.diamond(2)
kernelD3 = morph.diamond(3)
Ie = cv2.erode(A, kernelD2)
Id = cv2.dilate(Ie, kernelD3)
It would be so nice, if someone can help me.
Best Regards.
Tarcísio

How do I crop an image based on custom mask in python?

Below I have attached two images. I want the first image to be cropped in a heart shape according to the mask image (2nd image).
I searched for solutions but I was not able to get the simple and easier way to do this. Kindly help me with the solution.
2 images:
Image to be cropped:
Mask image:
Let's start by loading the temple image from sklearn:
from sklearn.datasets import load_sample_images
dataset = load_sample_images()
temple = dataset.images[0]
plt.imshow(temple)
Since, we need to use the second image as mask, we must do a binary thresholding operation. This will create a black and white masked image, which we can then use to mask the former image.
from matplotlib.pyplot import imread
heart = imread(r'path_to_im\heart.jpg', cv2.IMREAD_GRAYSCALE)
_, mask = cv2.threshold(heart, thresh=180, maxval=255, type=cv2.THRESH_BINARY)
We can now trim the image so its dimensions are compatible with the temple image:
temple_x, temple_y, _ = temple.shape
heart_x, heart_y = mask.shape
x_heart = min(temple_x, heart_x)
x_half_heart = mask.shape[0]//2
heart_mask = mask[x_half_heart-x_heart//2 : x_half_heart+x_heart//2+1, :temple_y]
plt.imshow(heart_mask, cmap='Greys_r')
Now we have to slice the image that we want to mask, to fit the dimensions of the actual mask. Another shape would have been to resize the mask, which is doable, but we'd then end up with a distorted heart image. To apply the mask, we have cv2.bitwise_and:
temple_width_half = temple.shape[1]//2
temple_to_mask = temple[:,temple_width_half-x_half_heart:temple_width_half+x_half_heart]
masked = cv2.bitwise_and(temple_to_mask,temple_to_mask,mask = heart_mask)
plt.imshow(masked)
If you want to instead make the masked (black) region transparent:
tmp = cv2.cvtColor(masked, cv2.COLOR_BGR2GRAY)
_,alpha = cv2.threshold(tmp,0,255,cv2.THRESH_BINARY)
b, g, r = cv2.split(masked)
rgba = [b,g,r, alpha]
masked_tr = cv2.merge(rgba,4)
plt.axis('off')
plt.imshow(dst)
Since I am on a remote server, cv2.imshow doesnt work for me. I imported plt.
This code does what you are looking for:
import cv2
import matplotlib.pyplot as plt
img_org = cv2.imread('~/temple.jpg')
img_mask = cv2.imread('~/heart.jpg')
##Resizing images
img_org = cv2.resize(img_org, (400,400), interpolation = cv2.INTER_AREA)
img_mask = cv2.resize(img_mask, (400,400), interpolation = cv2.INTER_AREA)
for h in range(len(img_mask)):
for w in range(len(img_mask)):
if img_mask[h][w][0] == 0:
for i in range(3):
img_org[h][w][i] = 0
else:
continue
plt.imshow(img_org)

Moments of many objects (Python OpenCV)

I have an image that contains ~400 dots. I've been using the Simpleblob detector to find the keypoints. I then for loop over all keypoints to find the center of each of the keypoints (code below). This works well, but I'm also interested in the moment information, as I would imagine .pt is only averaging the position of all of the pixels associated with the keypoints.
import cv2
import numpy as np
import csv
im = cv2.imread("8f3secshim.bmp", cv2.IMREAD_GRAYSCALE)
detector = cv2.SimpleBlobDetector_create()
keypoints = detector.detect(im)
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]),
(0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow("Keypoints", im_with_keypoints)
x = np.empty([len(keypoints), 2])
for i in range(len(keypoints)):
x[i] = keypoints[i].pt
I wanted to do something along the lines of this:
M = np.empty([lens(keypoints), 1])
for j in range(len(keypoints)):
M[j] = cv2.moments(keypoints[j])
but it fails.
I've tried abandoning the Simpledetector and using a treatment for moments listed here http://docs.opencv.org/trunk/dd/d49/tutorial_py_contour_features.html
, but that has failed as well.
If anyone has any suggestions they would be much appreciated.
I seem to have incorrectly worded my question. If anyone is looking to find the moments associated with individual objects found in a given image, the following code can be used.
import cv2
import numpy as np
img = cv2.imread('8f3secshim.bmp',0)
ret,thresh = cv2.threshold(img,127,255,0)
im2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
print(len(contours))
a = np.empty([len(contours), 1])
cx = np.empty([len(contours), 1])
cy = np.empty([len(contours), 1])
for i in range(0, len(contours)):
Mi = cv2.moments(contours[i])
#if any m00 moment is equal to zero the code can not be completed...
if Mi['m00'] != 0:
cx[i]= Mi['m10']/Mi['m00']
cy[i]= Mi['m01']/Mi['m00']
a[i] = cv2.contourArea(contours[i])
x = np.hstack((cx, cy, a))
#x is a len(contours), 3 matrix.
I'm sure there is probably a more elegant way to do this, but this works.

Filling holes in image with OpenCV or Skimage

I m trying to fill holes for a chessboard for stereo application. The chessboard is at micro scale thus it is complicated to avoid dust... as you can see :
Thus, the corners detection is impossible. I tried with SciPy's binary_fill_holes or similar approaches but i have a full black image, i dont understand.
Here is a function that replaces the color of each pixel with the color that majority of its neighbor pixels have.
import numpy as np
import cv2
def remove_noise(gray, num):
Y, X = gray.shape
nearest_neigbours = [[
np.argmax(
np.bincount(
gray[max(i - num, 0):min(i + num, Y), max(j - num, 0):min(j + num, X)].ravel()))
for j in range(X)] for i in range(Y)]
result = np.array(nearest_neigbours, dtype=np.uint8)
cv2.imwrite('result2.jpg', result)
return result
Demo:
img = cv2.imread('mCOFl.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
remove_noise(gray, 10)
Input image:
Out put:
Note: Since this function replace the color of corner pixels too, you can sue cv2.goodFeaturesToTrack function to find the corners and restrict the denoising for that pixels
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 30)
corners = np.squeeze(np.int0(corners))
You can use morphology: dilate, and then erode with same kernel size.
A faster, more accurate way is to use skimage.morphology.remove_small_objects docs
im = imread('a.png',cv2.IMREAD_GRAYSCALE)
im = im ==255
from skimage import morphology
cleaned = morphology.remove_small_objects(im, 200)

Overlay images using python library

I wanted to overlay one image over another dynamically using a Python library, and was wondering which one would be easy to use.
Thanks for your help!
If you want to overlay two images, simply use the OpenCV libraries.
[Sample]
Here is the sample python code using OpenCV to overlay image1 and image2
import cv2
import numpy as np
def overlay(image1, image2, x, y):
image1_gray = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
_, contours, _ = cv2.findContours(image1_gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
image1_mask = np.zeros_like(image1)
cv2.drawContours(image1_mask, contours, -1, (255,255,255), -1)
idx = np.where(image1_mask == 255)
image2[y+idx[0], x+idx[1], idx[2]] = image1[idx[0], idx[1], idx[2]]
return image2
if __name__ == '__main__':
grass_img = cv2.imread('grassland.jpg')
horse_img = cv2.imread('horse.png')
overlayed = overlay(horse_img, grass_img, 300, 300)
cv2.imwrite('overlayed.png', overlayed)
The result image was resized to reduce its volume, but in the above code, the resize code was omitted.
Result:
Update!
Here is the code using image's alpha value, and the output is better than before.
The idea is from overlay a smaller image on a larger image python OpenCv
def better_overlay(image1, image2, x, y):
image1_alpha = image1[:, :, 3] / 255.0
height, width = image1.shape[0], image1.shape[1]
for c in range(3):
image2[y:y+height, x:x+width, c] = image1_alpha * image1[:, :, c] + (1.0 - image1_alpha)* image2[y:y+height, x:x+width, c]
return image2
Result:
Usually PIL(Python Imaging Library) is for image processing stuff.

Categories