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.
Related
import numpy as np
from PIL import Image
import cv2
with Image.open("image.png") as im:
im = im.convert("CMYK")# not a true CMYK conversion here
im.show(title="image")
img = np.array(im)
#cv2.imshow('image', img)
I need to view a CMYK file hopefully using OpenCV and read pixel values in the CMYK space. I tried to load an image, convert it to CMYK(just 4 color levels) and view it using cv2. Note, I have cv2* commented out because it will cause Python to crash and OpenCv will need to be reinstalled. Will OpenCv allow me to view a (x, x, 0:3).uint8 numpy array? If so, throw me a line.
My solution was simple. I forgot the following:
cv2.waitKey(0)
cv2.destroyAllWindows()
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!
I am working on some image analysis in python using OpenCV. I want to display an image that I filled in holes with using scipy.ndimage.binary_filled_holes. Upon doing this I could not see anything being displayed when I used cv2.imshow, so I used plt.imshow and saw that the holes in my original image were filled. I want to use the cv2.imshow function to display the image. I did convert the image so that the datatype is uint8, yet still, nothing shows up. Any help would be appreciated.
import cv2
import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage
img = cv2.imread('Funky 647.jpg', cv2.IMREAD_GRAYSCALE)
dst = cv2.fastNlMeansDenoising(img,None,10,7,21)
ret, thresh2 = cv2.threshold(dst, 40, 255, cv2.THRESH_BINARY)
hole_filled= np.uint8(scipy.ndimage.binary_fill_holes(thresh2))
# plt.imshow(hole_filled)
cv2.imshow('No Holes', hole_filled)
cv2.waitKey(0)
cv2.destroyAllWindows()
Hole Filled Image via matplotlib:
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 /
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)