I have tried to convert an RGB image to Otsu binary image (gray scale) but that doesn't seem to work as I get the error as mentioned below.
from cv2 import cv2
import numpy as np
from skimage.color import rgb2gray
from skimage.filters import threshold_otsu
from skimage.io import imread
from skimage.morphology import skeletonize
from skimage.util import invert
import matplotlib.pyplot as plt
img = rgb2gray(imread('Ared.png'))
binary = img > threshold_otsu(img)
np.unique(binary)
skeleton = skeletonize(invert(binary))
cv2.imshow('original', img)
cv2.imshow('skeleton', skeleton)
cv2.waitKey(0)
cv2.destroyAllWindows()
Result in terminal
img = rgb2gray(imread('Ared.png'))
Traceback (most recent call last):
File "preprocessing.py", line 16, in <module>
cv2.imshow('skeleton', skeleton)
TypeError: Expected Ptr<cv::UMat> for argument '%s'
Your format is wrong. You need to change it to float32. That's a common error with opencv. You can change this line to convert it to float32 and it should work fine.
cv2.imshow('skeleton', np.float32(skeleton))
Bad exception messages are bad...
cv2.imshow does not handle binary arrays. The acceptable types according to this answer are uint8, uint16, int, float, and double.
you should be able to convert the array to uint8 with:
skeleton.astype('u1')
This will leave you with values between 0 and 1 though, which is all very dark. If you the multiply the array by 255, the colors should be black and white as expected:
skeleton.astype('u1') * 255
full example with data image from skimage:
from cv2 import cv2
from skimage.filters import threshold_otsu
from skimage.morphology import skeletonize
from skimage.util import invert
from skimage.data import camera
img = camera()
binary = img > threshold_otsu(img)
skeleton = skeletonize(invert(binary))
cv2.imshow('original', img)
cv2.imshow('skeleton', skeleton.astype('u1')*255)
cv2.waitKey(0)
cv2.destroyAllWindows()
Related
I am trying to load this image into python (I provided a link because its 75mb): https://drive.google.com/file/d/1usiKRN1JQaIxTTo_HTXPwUj8LeyR8CDc/view?usp=sharing
My current code is below, it loads the image and when you png.show() the image it displays it in RBG but when converted to a numpy array its shape is only (h, w) not (h, w, 3) for RGB.
Code:
import numpy as np
from PIL import Image
Image.MAX_IMAGE_PIXELS = 233280000
png = Image.open('world.png')
png.show()
png = np.array(png)
print(png.shape)
Try this instead:
import numpy as np
from PIL import Image
Image.MAX_IMAGE_PIXELS = 233280000
png = Image.open('world.png').convert('RGB')
png.show()
png = np.array(png)
print(png.shape)
I am trying to extract text from the image, however, with the following code that I have tried on other images, it works but not on this image. Is there an issue with the code?
Image trying extract text from:
Here is the code:
import cv2
import pytesseract
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
try:
from PIL import Image
except ImportError:
import Image
# Import image, convert,resize and noise removal
img = cv2.imread("sample01.png", cv2.IMREAD_GRAYSCALE)
print('Dimension of image: {}'.format(img.ndim))
img = cv2.resize(img, None, fx=2, fy=2)
blur = cv2.GaussianBlur(img, (5, 5), 0)
# Apply adaptiveThreshold (Mean)
th2 = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 11, 2)
cv2.imwrite('resize_adaptive_threshmean.png', th2)
# Apply Tesseract to detect words
print(pytesseract.image_to_string(Image.open('resize_adaptive_threshmean.png')))
print("=========================================================")
Is there anything wrong with the code?
Well, you can use adaptive-thresholding
import cv2
import numpy as np
import pytesseract
img = cv2.imread("ACtBA.png")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
flt = cv2.adaptiveThreshold(gry,
100, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 15, 16)
txt = pytesseract.image_to_string(flt)
print(txt)
The image will be:
Result:
Parking: You may park anywhere on the campus where there are no signs prohibiting par-
king. Keep in mind the carpool hours and park accordingly so you do not get blocked in the
afternoon
Under Schoo! Age Children.While we love the younger children, it can be disruptive and
inappropriate to have them on campus during school hours. There may be special times
that they may be invited or can accompany a parent volunteer, but otherwise we ask that
you adhere to our —_ policy for the benefit of the students and staff.
I test with different parameters, so I think the most suitable parameters are:
maxValue = 100 # Display pixels greater than maxValue
blockSize=15. # size of neighbourhood area.
C=16 # just a constant which is subtracted from the mean or weighted mean calculated.
import cv2
import pytesseract
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
try:
from PIL import Image
except ImportError:
import Image
data = pytesseract.image_to_string(Image.open("sample01.png"))
print(data)
?
Able to show the image through matplotlib, however unable to do it through cv2.imshow. The shape of the image is not consistent with opencv required formats. Require help on changing on changing it so it can be shown by the command cv2.imshow
test.jpg is a random jpg file from web
import numpy as np
import cv2
import matplotlib.pyplot as plt
import ReadIM
img = cv2.imread('test.jpg')
vbuff, vatts = ReadIM.extra.get_Buffer_andAttributeList('test.im7')
v_array, vbuff = ReadIM.extra.buffer_as_array(vbuff)
print (np.shape(v_array))
print (v_array[0])
print (np.shape(img))
# Showing image through matplotlib
plt.imshow(v_array[0])
plt.show()
#Showing image through cv2
cv2.imshow('image',v_array[0])
cv2.waitKey(0)
cv2.destroyAllWindows()
# Remove memory
#del(vbuff)
ReadIM.DestroyBuffer(vbuff)
ReadIM.DestroyAttributeListSafe(vatts)
test.im7
Normalizing the image to (0,255) will do the trick
img = cv2.normalize(img, None, 255,0,cv2.NORM_MINMAX,dtype = cv2.CV_8UC1)
cv2.imshow('image',img)
I want to do a matchTemplate from a screenshot (with mss)
from mss import mss
import cv2
import numpy
with mss() as sct:
screenshot_numpy = numpy.array(sct.shot())
template = cv2.imread('./templates/player.png')
result = cv2.matchTemplate(screenshot_numpy,template,cv2.TM_CCOEFF_NORMED)
Error message:
Traceback (most recent call last):
File "main.py", line 14, in <module>
result = cv2.matchTemplate(screenshot_numpy,template,cv2.TM_CCOEFF_NORMED)
TypeError: image data type = 18 is not supported
From the mss examples page:
img = numpy.array(sct.grab(monitor))
So here we can see the .grab() method to get the raw pixel data from the image. In this case sct.grab() returns a PIL Image, and numpy.array(Image) will thus convert the PIL Image object into a numpy ndarray.
Check the numpy ndarray dtype after you convert; for e.g. if your code is ndarray_img = numpy.array(sct.grab()), then check ndarray_img.dtype. If it's np.uint8 then you're done. If it's np.uint16, then you'll have to divide by 256 and convert to np.uint8 with ndarray_img = (ndarray_img/255).astype(np.uint8).
Further down you'll see another example which flips the R and B channels of the image:
cv2.imshow(title, cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
except this is actually backwards. It really doesn't matter because either way it's just swapping the first and third channel, so BGR2RGB and RGB2BGR do exactly the same thing, but PIL (and other libraries) give you RGB order while you need BGR order to display with OpenCV, so technically it should be
cv2.imshow(title, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
Here i'm trying to:
- Apply Adaptive filtering to image .
- Enhance the contrast of image .
my code is as follow :
#!/usr/bin/python
import cv2
import numpy as np
import PIL
from PIL import ImageFilter
from matplotlib import pyplot as plt
img = cv2.imread('Crop.jpg',0)
cv2.imshow('original',img)
img = cv2.medianBlur(img,5)
th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)
cv2.imshow('image',th3)
th3 = th3.filter(ImageFilter.SMOOTH)
cv2.imshow('image',th3)
cv2.waitKey(0)
cv2.destroyAllWindows()
I am getting following Error :
Traceback (most recent call last):
File "./adaptive.py", line 22, in
th3 = th3.filter(ImageFilter.SMOOTH)
AttributeError: 'numpy.ndarray' object has no attribute 'filter
You may confuse with cv.Smooth