Im just trying some things with OpenCV. Once i try to to put a rectangle on my image, I always receive the following error: Expected Ptr<cv::UMat> for argument 'img'
This happens while using the following code:
!pip install opencv-python
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import cv2
img = cv2.imread("C:/..../.../.../.../.....jpg")[...,::-1]
cv2.rectangle(img,(400,200),(500,500),(0,0,255),20)
plt.imshow(img)
plt.show()
Is there anybody who faced the same issue once?
Thanks for your help in advance
Best regards
Sascha
If you want to display with matplotlib, the best way to change from BGR to RGB is to use
img = cv2.imread("C:/..../.../.../.../.....jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
instead of
img = cv2.imread("C:/..../.../.../.../.....jpg")[...,::-1]
I tried it and it works.
Related
I went to read up the syntax of cv2.imread() method and it says that specifying the flag=0 will load the image in grayscale.
The original image is this:
Original Image
And I executed the following code with the following libs, no errors.
import cv2
import pytesseract
import matplotlib
import image
img=cv2.imread("C:/Users/HP_Demo/Desktop/cv2/sample02.png",0)
plt.imshow(img)
plt.show()
The result is this:
Result image
import cv2
img=cv2.imread("colorful.png",1)
cv2.imshow("",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Result:
import cv2
img=cv2.imread("colorful.png",0) # same image changed the 1 to 0
cv2.imshow("",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Result:
Conclusion
As I said in a comment maybe the image you used is causing the no grayscale.
I do not really know but try using that: cv2.imread("path", cv2.IMREAD_GRAYSCALE).
PS. And it is better to use sys.path.join() instead of raw /
This is the image that I will import
My python code
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
print(pytesseract.image_to_string(Image.open('/home/milenko/Pictures/Screenshot from 2018-03-06 19-03-19.png')))
When I run code
python a72.py
As an output I got empty line.It does not make any sense.
Why?
Try to tweak your command a little bit using e.g.: other Page Segmentation Method As you can see the default value is "Fully automatic page segmentation, but no OSD." so it does not perform orientation and script detection (OSD).
This one gives me some output:
print(pytesseract.image_to_string(Image.open('image.png'), config='-psm 12'))
You can use OpenCV to prepare this image for OCR, e.g:
#!/usr/bin/python
import cv2 as cv
import numpy as np
import pytesseract
import Image
from matplotlib import pyplot as plt
img = cv.imread('/tmp/image.png',0)
ret,thresh = cv.threshold(img, 220, 255, cv.THRESH_BINARY)
plt.axis('off')
plt.imshow(thresh, 'gray')
plt.show()
print(pytesseract.image_to_string(thresh, config='-psm 12'))
In the next step you could divide this image into some parts (x-axis, y-axis, trend line) and use OCR for each part separately with the proper PSM value set for each one of them.
I am using the following code:
import cv2
import numpy as np
import pyautogui
import sys
img = pyautogui.screenshot()
cv2.imshow('image',img)
When I run this, it tells me
mat is not a numpy array, neither a scalar
I have tried to use different functions from opencv and it seems they all return the same. What do I need to do in order to take a screenshot then work with it in Open CV?
After some digging, I realise that the pyautogui function is using Pillow which is giving a format that must be adapted to work with opencv.
I added the following code so that it worked:
open_cv_image = np.array(img)
# Convert RGB to BGR
open_cv_image = open_cv_image[:, :, ::-1].copy()
I'm reading in an image with OpenCV, and trying to do something with it in numpy (rotate 90deg). Viewing the result with imshow from matplotlib, it all seems to be working just fine - image is rotated. I can't use drawing methods from OpenCV on the new image, however. In the following code (I'm running this in a sagemath cloud worksheet):
%python
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os, sys
image = np.array( cv2.imread('imagename.png') )
plt.imshow(image,cmap='gray')
image = np.array(np.rot90(image,3) ) # put it right side up
plt.imshow(image,cmap='gray')
cv2.rectangle(image,(0,0),(100,100),(255,0,0),2)
plt.imshow(image,cmap='gray')
I get the following error on the cv2.rectangle() command:
TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)
The error goes away if I use np.array(np.rot90(image,4) ) instead (i.e. rotate it 360). So it appears that the change in dimensions is messing it up. Does OpenCV store the dimensions somewhere internally that I need to update or something?
EDIT: Adding image = image.copy() after rot90() solved the problem. See rayryeng's answer below.
This is apparently a bug in the Python OpenCV wrapper. If you look at this question here: np.rot90() corrupts an opencv image, apparently doing a rotation that doesn't result back in the original dimensions corrupts the image and the OP in that post experiences the same error you are having. FWIW, I also experienced the same bug.... no idea why.
A way around this is to make a copy of the image after you rotate, and then show the image. This I can't really explain, but it seems to work. Also, make sure you call plt.show() at the end of your code to show the image:
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os, sys
image = np.array( cv2.imread('imagename.png') )
plt.imshow(image,cmap='gray')
image = np.array(np.rot90(image,3) ) # put it right side up
image = image.copy() # Change
plt.imshow(image,cmap='gray')
cv2.rectangle(image,(0,0),(100,100),(255,0,0),2)
plt.imshow(image,cmap='gray')
plt.show() # Show image
I faced the same problem with numpy 1.11.2 and opencv 3.3.0. Not sure why, but this did the job for me.
Before using cv2.rectangle, add the line below:
image1 = image1.transpose((1,0)).astype(np.uint8).copy()
Reference
Convert data type works for my problem.
The image is of type np.int64 before the convert.
image = image.astype(np.int32) # convert data type
I am looking for a way to rescale the matrix given by reading in a png file using the matplotlib routine imread,
e.g.
from pylab import imread, imshow, gray, mean
from matplotlib.pyplot import show
a = imread('spiral.png')
#generates a RGB image, so do
show()
but actually I want to manually specify the dimension of $a$, say 200x200 entries, so I need some magic command (which I assume exists but cannot be found by myself) to interpolate the matrix.
Thanks for any useful comments : )
Cheers
You could try using the PIL (Image) module instead, together with numpy. Open and resize the image using Image then convert to array using numpy. Then display the image using pylab.
import pylab as pl
import numpy as np
from PIL import Image
path = r'\path\to\image\file.jpg'
img = Image.open(path)
img.resize((200,200))
a = np.asarray(img)
pl.imshow(a)
pl.show()
Hope this helps.