when i try to use cv2.imshow() it gives me an empty window with the title i gave it. i tried another image with a link to the image, that did not work either. When i run print(img.shape) it gives me the size, so the image is loaded. here is my code:
import cv2
img = cv2.imread('image.jpg')
cv2.imshow('img', img)
cv2.waitKey(0)
here is what i get when i run this script:
image
if anyone knows something about this then please let me know!
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 code that I am using for OpenCV to display image. It only shows me a blank screen instead of showing a picture.
import cv2
# location and name of file is completely correct
img = cv2.imread("./Resources/img-2.jpg")
# Doesn't give a null so its okay
print(img.shape)
# suspecting that problem is here
cv2.imshow("preview", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
The image is stored in the right location and when I'm using a similar approach for a video and a webcam, it works perfectly.
The following is what the out is -
Try using matplotlib instead :
import matplotlib.pyplot as plt
import cv2
img = cv2.imread("./Resources/img-2.jpg")
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # convert img pixels to RGB format, so that matplotlib displays the image properly
plt.imshow(img)
plt.show()
If it still gives you a blank image, then the problem might come from your file or filename.
I'm trying simple code that read an image and convert it to grey scale then show both of them, and finally save the grey scale image and display it after saving. The problem is that cv2.imshow (image show) for saved image doesn't work.
The images before image writing are displayed correctly and the image saved correctly in the same path but can't be displayed using cv2.imshow.
'''
python
'''
import cv2
img=cv2.imread('cover.jpg')
cv2.imshow('image', img)
img_grey = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
cv2.imshow('image_grey', img_grey)
savedimage='new.jpg'
cv2.imwrite('new.jpg',img_grey)
cv2.imshow('testsavedimage',savedimage)
cv2.waitKey(0)
I receive error for showing saved image
File "C:/1.py", line 8, in <module>
cv2.imshow('testsavedimage',savedimage)
TypeError: Expected Ptr<cv::UMat> for argument '%s
savedimage is just a string in this case. If you want to make sure that your grey scale image was saved properly, you need to first read it back into a Mat object:
cv2.imwrite('new.jpg',img_grey)
savedImg = cv2.imread('new.jpg')
cv2.imshow('testsavedimage', savedImg)
Hope this helps.
(PS #Mark Setchel is right, you should be using cv2.COLOR_BGR2GRAY here. This is because imread() and imshow() default to BGR colorspace, not RGB)
I'am a newer for opencv.
I used the next command install the opencv 3.4
py -3 -m pip install open_python-3.4.4.19-cp36-cp36m-win_ame64.whl
and use the code named Image_Thresholding.ipynb process the picture
the file Image_Thresholding.ipynb like this:
import cv2
pic = cv2.imread('adult.jpg', 0)
threshold_value = 200
(T_value, binary_threshold) = cv2.threshold(pic, threshold_value, 255, cv2.THRESH_BINARY)
cv2.imshow('binary', binary_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()
I used pycharm2018.3.1 and set the configurations to use Jupter Notebook to run up code show the processed the picture, It's a white and black picture but I want use the next code
to show the original picture it also show the processed white and black picture
import cv2
img = cv2.imread('adult.jpg', 0)
cv2.imshow('adult', img)
cv2.waitKey(0)
cv2.destoryAllWindows()
How to show the original picture. Is my environment wrong or any else problem ?
You're experiencing such issue because of the 0 flag you use in the imread function call. That is used to read images in grayscale.
In order to load and display a color image just do:
import cv2
img = cv2.imread('adult.jpg')
cv2.imshow('adult', img)
cv2.waitKey(0)
cv2.destoryAllWindows()
Hope this helps
The following code does not display the image lists.jpg (in current dir):
print(dir(Image)) displays components; im.size, im.filename, im.format all return correct values.
What have I not done to display this jpg file?
from PIL import Image
im = Image.open("lists.jpg")
im.show() # did not work - perhaps due to the environment Jupyter Notebooks
Solution: replaced module with another with immediate results.
from IPython.display import Image
Image(filename='lists.jpg')
I know it is quite late to post but I will do it for new readers.
This problem arises in case of Jupyter Notebooks. Using show() does not display the image. So discard calling show() like in the code below. This will display the image in the output of the cell.
from PIL import Image
im = Image.open("lists.jpg")
im
To display the image on screen:
from PIL import Image
im = Image.open("lists.jpg")
im.show()
See also http://pillow.readthedocs.io/en/4.0.x/reference/Image.html