Related
I am trying to implement the Pikachu image on the hoarding board using warpPerspective transformation. The output doesn't have smooth edges it has dotted points instead.
import cv2
import numpy as np
image = cv2.imread("base_img.jpg")
h_base, w_base = image.shape[0], image.shape[1]
white_subject = np.ones((480,640,3),dtype="uint8")*255
h_white, w_white = white_subject.shape[:2]
subject = cv2.imread('subject.jpg')
h_sub, w_sub = subject.shape[:2]
pts2 = np.float32([[109,186],[455,67],[480,248],[90,349]])
pts3 = np.float32([[0, 0], [w_white, 0], [w_white, h_white], [0, h_white]])
transformation_matrix_white = cv2.getPerspectiveTransform(pts3, pts2)
mask = cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base))
image[mask==255] = 0
pts3 = np.float32([[0, 0], [w_sub, 0], [w_sub, h_sub], [0, h_sub]])
transformation_matrix = cv2.getPerspectiveTransform(pts3, pts2)
warped_image = cv2.warpPerspective(subject, transformation_matrix, (w_base, h_base))
Hoarding board image
Pikachu Image
Output Image
Pattern Image
Output Image
Please help me out in getting the output without the dotted point at the edges.
Here is one way to do the anti-aliased composite in Python/OpenCV. Note that I use the background color of the overlay image in the borderVal constant in warpPerspective to set the background color, since it is a constant. I also blur the mask before doing the composite.
Background Image:
Overlay Image:
import cv2
import numpy as np
import skimage.exposure
image = cv2.imread("base_img.jpg")
h_base, w_base = image.shape[0], image.shape[1]
white_subject = np.ones((480,640,3),dtype="uint8")*255
h_white, w_white = white_subject.shape[:2]
subject = cv2.imread('subject.jpg')
h_sub, w_sub = subject.shape[:2]
# get background color from first pixel at (0,0) and its BGR components
yellow = subject[0:1, 0:1][0][0]
blue = yellow[0]
green = yellow[1]
red = yellow[2]
print(yellow)
print(blue, green, red)
pts2 = np.float32([[109,186],[455,67],[480,248],[90,349]])
pts3 = np.float32([[0, 0], [w_white, 0], [w_white, h_white], [0, h_white]])
transformation_matrix_white = cv2.getPerspectiveTransform(pts3, pts2)
mask = cv2.warpPerspective(white_subject, transformation_matrix_white, (w_base, h_base))
pts3 = np.float32([[0, 0], [w_sub, 0], [w_sub, h_sub], [0, h_sub]])
transformation_matrix = cv2.getPerspectiveTransform(pts3, pts2)
# do warping with borderVal = background color
warped_image = cv2.warpPerspective(subject, transformation_matrix, (w_base, h_base), borderMode = cv2.BORDER_CONSTANT, borderValue=(int(blue),int(green),int(red)))
# anti-alias mask
mask = cv2.GaussianBlur(mask, (0,0), sigmaX=2, sigmaY=2, borderType = cv2.BORDER_DEFAULT)
mask = skimage.exposure.rescale_intensity(mask, in_range=(0,128), out_range=(0,255))
# convert mask to float in range 0 to 1
mask = mask.astype(np.float64)/255
# composite warped image over base and convert back to uint8
result = (warped_image * mask + image * (1 - mask))
result = result.clip(0,255).astype(np.uint8)
# save results
cv2.imwrite('warped_mask.png',(255*mask).clip(0,255).astype(np.uint8))
cv2.imwrite('warped_image.png',warped_image)
cv2.imwrite('warped_image_over_background.png',result)
cv2.imshow("mask", mask)
cv2.imshow("warped_image", warped_image)
cv2.imshow("result", result)
cv2.waitKey(0)
Anti-aliased Warped Mask:
Warped Image:
Resulting Composite:
i am new in python , my probleme it's about edit some changes in an image grayscale , i wanna make a binarization for this image , the values of pixels bigger then 100 take the value 1 (white), and the values low than 100 takes the value 0 (black)
so any suggestion plz (sorry for my bad english)
my code :
`import numpy as np
import cv2
image = cv2.imread('Image3.png', 0)
dimension = image.shape
height = dimension[0]
width = dimension[1]
#finalimage = np.zeros((height, width))
for i in range(height) :
for j in range(width):
if (image[i, j] > 100):
image[i][j] = [1]
else:
image[i][j] = [0]
cv2.imshow('binarizedImage',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
You can try use OpenCV function cv2.threshold for binarize.
import cv2
img = cv2.imread('Image3.png', cv2.IMREAD_GRAYSCALE)
thresh = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)[1]
cv2.imshow('binarizedImage',thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
I think you just want to use np.where():
import numpy as np
image = np.array([[200, 50, 200],[50, 50 ,50],[10, 255, 10]]) #this will be your image instead
In [11]: image
Out[11]:
array([[200, 50, 200],
[ 50, 50, 50],
[ 10, 255, 10]])
In [12]: np.where(image > 100, 1, 0)
Out[12]:
array([[1, 0, 1],
[0, 0, 0],
[0, 1, 0]])
I would like to extract the time from some images using pytesseract in Python, but it doesn't produce anything.
The code I was using is as follow:
import pytesseract
from PIL import Image, ImageOps
im = Image.open(r'im.jpg')
im_invert = ImageOps.invert(im)
text = pytesseract.image_to_string(im_invert)
print(text)
The original image:
Image after inversion operation:
When I ran the code above, the only thing I got is
Is there anything wrong with my code?
If you can use EasyOCR, then this approach below works for your input image.
I have tested the given original image in google colab. For showing output images locally use cv2.imshow(...) and cv2.waitkey(0).
Here, first median blur is applied to grayscale image. Next, thresholding, erosion and dilation is applied. Median Blur + Thresholding outputs almost similar confidence as Median Blur + Thresholding + Erosion + Dilation in this case.
Image
OCR Prediction Including Confidence
Thresholding:
[([[3, 1], [270, 1], [270, 60], [3, 60]], '09:01:00', 0.797291100025177)]
Erosion:
[([[2, 2], [270, 2], [270, 58], [2, 58]], '09:01:00', 0.4145631492137909)]
Dilation:
[([[3, 1], [270, 1], [270, 60], [3, 60]], '09:01:00', 0.7948697805404663)]
Code
import cv2
import easyocr
import numpy as np
from PIL import Image
from google.colab.patches import cv2_imshow
# need to run only once to load model into memory
reader = easyocr.Reader(['ch_sim','en'])
img = cv2.imread('1.jpg', 0)
img = cv2.medianBlur(img, 5)
ret, th1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
#th1 = cv2.bitwise_not(th1)
kernel = np.ones((3,3), np.uint8)
erosion = cv2.erode(th1, kernel, iterations = 1)
dilation = cv2.dilate(erosion, kernel, iterations = 1)
print("Thresholding:\n")
cv2_imshow(th1)
print("\nErosion:\n")
cv2_imshow(erosion)
print("\nDilation:\n")
cv2_imshow(dilation)
print("Thresholding:")
result = reader.readtext(th1)
print(result)
print("Erosion:")
result = reader.readtext(erosion)
print(result)
print("Dilation:")
result = reader.readtext(dilation)
print(result)
I am trying to standardize and then normalise an image using Numpy and OpenCV in the following manner; however, the image that's output from matplotlib looks identical. Why is that?
Code
%matplotlib inline
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
IMG_SIZE = 256
def show_img(img):
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
imgplot = plt.imshow(img_rgb)
img = cv2.imread('/content/drive/My Drive/ai/test_images/test_image3.tif')
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
show_img(img)
img = img.astype('float32')
img = (img - img.mean(axis=(0, 1, 2), keepdims=True)) / img.std(axis=(0, 1, 2), keepdims=True)
img = cv2.normalize(img, None, 0, 1, cv2.NORM_MINMAX)
show_img(img)
It is a normal behaviour. Normalizing an image means to change its ranges from 0-255 to 0-1. Matplotlib checks the range of the RGB values and display the image accordingly.
As for standardisation, if you look closely you can see a color shift. Orange seems a little lighter on the second image.
These behaviours are normal because preprocessing should not change the image (at least the information inside) but should help the model to processed them ALL.
import scipy.ndimage.morphology as m
import numpy as np
import cv2
def skeletonize(img):
h1 = np.array([[0, 0, 0],[0, 1, 0],[1, 1, 1]])
m1 = np.array([[1, 1, 1],[0, 0, 0],[0, 0, 0]])
h2 = np.array([[0, 0, 0],[1, 1, 0],[0, 1, 0]])
m2 = np.array([[0, 1, 1],[0, 0, 1],[0, 0, 0]])
hit_list = []
miss_list = []
for k in range(4):
hit_list.append(np.rot90(h1, k))
hit_list.append(np.rot90(h2, k))
miss_list.append(np.rot90(m1, k))
miss_list.append(np.rot90(m2, k))
img = img.copy()
while True:
last = img
for hit, miss in zip(hit_list, miss_list):
hm = m.binary_hit_or_miss(img, hit, miss)
img = np.logical_and(img, np.logical_not(hm))
if np.all(img == last):
break
return img
img = cv2.imread("e_5.jpg",0)
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)
skel = skeletonize(img)
imshow(skel, cmap="gray", interpolation="nearest")
I have been trying this code inorder to skeletonize an image without any gaps in between the skeleton lines. But whenever I run the program , an error is hitting"imshow in not defined".
I tried plt.imshow, at this point, there was neither an error nor an output image. Can anyone tell me where am going wrong.
You may have forgotten to import the module using as short alias plt (note that the name can be replaced by what you wants), AND call the show() command so add the following to your code:
import matplotlib.pyplot as plt
plt.imshow()
plt.show()
see for more information: https://stackoverflow.com/a/3497922/4716013