I am trying to transpose an image using opencv,python but when ı set the destination for it, it doesnt write to it so when ı look at output image ı only see a black screen. Why does that happen?
Here's my code;
import cv2
import numpy as np
a=np.zeros(image.shape).astype(image.dtype)
cv2.transpose(image,a)
cv2.imwrite("a.png",a)
cv2.imshow("hh",a)
cv2.waitKey(0)
cv2.destroyAllWindows()
documentation https://docs.opencv.org/master/d2/de8/group__core__array.html#ga46630ed6c0ea6254a35f447289bd7404
OpenCV is sensitive to matrices it can't completely modify (resize). it can do that for cv::Mat. it can't for numpy arrays.
simply use a = cv2.transpose(image)
Related
i am newbie to python. I am trying to create a Python Program to image dehazing using dcp. I have an image that need to view at console at first and need to do some dehazing method. unfortunately, here i unable to upload or view the image and it saying Image data cannot be converted to float. I am getting the following error when I try running it.
import cv2
import math
import numpy as np
import matplotlib.pyplot as plt
def DarkChannel(im,sz):
b,g,r = cv2.split(img)
dc = cv2.min(cv2.min(r,g),b)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (sz,sz))
dark = cv2.erode(dc,kernel)
return dark
img = cv2.imread("C:/Users/User/Documents/sypder/img/bird.jpg", 1)
plt.imshow(img)
It seems your file path is wrong since your sample code worked perfectly for me. If you are struggling with file paths you can pass it as a raw string.
img = cv2.imread(r"C:\Users\User\Documents\sypder\img\bird.jpg", 1)
If you fix it like this, it should work. I copied the object name from properties while doing it.
try to read this post :
https://www.pythonfixing.com/2021/10/fixed-typeerror-image-data-can-not.html
it's jupiter based article but you can change them accordingly
I'm a newbie too that come across your question but those might be a help
goodluck!
In my python course, the instructor uploads a greyscale picture of himself and reads it on Python with the following code:
import numpy as np
import math
from PIL import Image
from IPython.display import display
im = Image.open("chris.tiff")
array = np.array(im)
print(array.shape)
and he gets
(200,200)
When I write the code and run my own image, with the exact same extension "tiff", I get a 3-dimensional array. I was told it's because my image was colored and so the third entry is for RBG. So I used a greyscale photo just like he did but I still obtain a 3D array, why?
Any help is greatly appreciated, thank you
EDIT
For extra clarity, the array I get for my greyscale image with tiff extension is
(3088, 2316, 4)
Your photo appears to be grey, but actually, it has the three channels based on the posted shape.
So, you need to convert it to greyscale using the following line:
im = Image.open("chris.tiff").convert('L')
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 fairly new with python so I'm not really sure where to start. All I have done is I imported the text file to python. I was suggested to use matshow.py but I don't know how I would use the text file to create the actual image.
If you can contrive to get your text file into a numpy array then this kind of code will work. Just research, or ask another question, about reading a text file into a numpy array.
>>> from PIL import Image
>>> import numpy as np
>>> pic = np.zeros((100,100), dtype=np.int8)
>>> image = Image.fromarray(pic)
>>> image.show()
Here I import the Image class from the PIL library. Then I create a 100x100 array of zeroes in pic using numpy. I use a method from Image to make this into an Image object and then display the (utterly uninteresting, completely black) result.
What exactly are you trying to achieve, your point isn't clear.
if you are trying to draw an image using a matrix.
you will need to look into PIL.
Here is the link to download the library and here is the link for the documentations.
Try
Matrix = loadtxt(filename)
imshow(Matrix)
These functions come from the numpy and matplotlib libraries, respectivelly.
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