I am implementing gabor kernels, when I display the kernels while running the code (before saving them) they give me a picture like this
But after saving the kernels as jpg images using cv2.imwrite, I get like that
Any explanations? and how to save the kernels as in the first image?
There could be different causes. So I have two suggestions:
If you display the first picture with plt.imshow(), export it with plt.savefig(). This should easily be working.
If you still want to export the image with cv2.imwrite() make sure that the picture is correctly rescaled first. (mind that if you have only one channel, you will get a grayscale picture).
If we call the original picture org_img:
img = org_img
min_val,max_val=img.min(),img.max()
img = 255.0*(img - min_val)/(max_val - min_val)
img = img.astype(np.uint8)
cv2.imwrite(img,"picture.png")
Related
I am reading a image using open cv and when it shows the image it zooms it in. The image is 1080 x 1257. I also tried another image with dimension 5961 x 3059 it zooms it in even more. If I use img=cv2.imread("hello.jpg",50)
it shows the first image in original dimension but is of grayscale, but the second image is still not in original dimension. So how do I display original dimension images
Help me with this as am I an absolute beginner with OpenCV.
here is the second image I was talking about.
here is the output of the image
img=cv2.imread("rainbow.png",0)
cv2.imshow('israinbow',img)
cv2.waitKey(5000)
cv2.destroyAllWindows()
I tried loading and saving images with python using cv2,PIL, scipy , but the saved image has a bit different color compare to the original.
I am loading and saving tif format, so i expect no color change.
link to the image I am using:
https://data.csail.mit.edu/graphics/fivek/img/tiff16_c/a0486-jmac_MG_0791.tif
the difference between loaded image and saved image is:
can you help me understand what I am doing wrong? why the color change?
update:
the problem is because the image is prophoto rgb color.
does anyone knows how can i convert a batch of images from prophoto rgb to rgb?
thanks,
yoav
option 1:
img = imread(file_name)
imsave('imread.tif', img)
option 2:
img = cv2.imread(file_name)
cv2.imwrite('cv2.tif', img)
option 3:
img = Image.open(file_name)
img.save('pil.tif')
I think OpenCV is more interested in Computer Vision - i.e. detecting and measuring objects etc than printing or high quality image reproduction, editing and printing, so it pretty much ignores ICC profiles. If anyone knows better, I am happy to be corrected.
You can use ImageMagick to convert images from one format to another, and to do many, many other things, one of which is changing colour profiles. So, I think, if you go to this website and download an sRGB profile (I chose the first one with "preference" in its name) and save it as sRGB.icc, you can change one of your ProPhoto images to a normal sRGB image with the following command in Terminal:
convert input.tif -profile sRGB.icc output.tiff
Try that and see if it works. If so, make a copy of your images and on a copy, you can run mogrify to do the whole lot in one go - beware and make a copy like I suggest because it will very quickly alter all your images...
magick mogrify -profile sRGB.icc *tif
You can see the embedded profile and loads of other information about an image using ImageMagick's identify command:
magick identify -verbose OneOfYourImages.tiff
I am working on a project where our database is on svg format. Now, the weird thing (I don't know much about image formats) is that the image looks totally different if I open it on a computer program like Image Viewer, ImageMagick or Pinta, to how the image looks when I open it on a browser (be it Mozilla or Chrome). I am attaching an image (converted from svg to png) for convenience and you can see that the difference is really big if you open it on a browser compared to a normal program (if you download it and open it on your computer).
Now, I opened it on Python to see what is going on, and apparently the image is on RBGA format. I thought to convert it to RGB, and did it using the following code:
img = Image.open(os.path.join(PNG_REPO, page)[:-4] + ".png")
arr = np.array(img)
rgbImage = cv2.cvtColor(arr, cv2.COLOR_RGBA2RGB)
img = Image.fromarray(rgbImage)
img.save("please.png")
and then, weird stuff happened. The image seems to have become completely black (attached below) and opening it on Python, a local program or a browser doesn't make any difference anymore.
Anyone has any idea what is going on? I think that I am losing it and I am completely stucked.
In fact, SVG and PNG are totally different.
SVG (Scallable vector graphics) is not a standard image format : the image is not stored as an raay of pixel (or derivative), but as a vectorized format. To display it, you would have to render the vectors into pixels, then display them. The big assets of this approach are the quality vs. size : for illustrations, you can have a fantastic quality for almost nothing in size, however, it requires more time to process, and the decoding is very different from other image formats, thus it is often not supported by image viewers, and the renders can depend a bit on the renderer.
PNG (Portable network graphics) on the other hand is still based on pixel, and is very widely supported.
As for your question, your image have an alpha (transparency) channel. When converting it to RGB, you are getting rid of this transparency, which opencv translates into a fixed color. Here, this color is (0,0,0), that is to say black.
You should try this :
img = Image.open(os.path.join(PNG_REPO, page)[:-4] + ".png")
img.load()
new_img = Image.new("RGB", img.size, (255, 255, 255))
new_img.paste(img, mask=img.split()[3]) # 3 is the alpha channel
new_img.save("should_be_good.png")
I've got a pretty strange issue. I have several tif images of astronomical objects. I'm trying to use opencv's python bindings to process them. Upon reading the image file, it appears that segments of the images are swapped or rotated. I've stripped it down to the bare minimum, and it still reproduces:
img = cv2.imread('image.tif', 0)
cv2.imwrite('image_unaltered.tif', img)
I've uploaded some samples to imgur, to show the effect. The images aren't super clear, that's the nature of preprocessed astronomical images, but you can see it:
First set:
http://imgur.com/vXzRQvS
http://imgur.com/wig99KR
Second set:
http://imgur.com/pf7tnPz
http://imgur.com/xGn9C77
The same rotated/swapped images appear if I use cv2.imShow(...) as well, so I believe it's something when I read the file. Furthermore, it persists if I save as jpg as well. Opening the original in Photoshop shows the correct image. I'm using opencv 2.4.10, on Linux Mint 17.1. If it matters, the original tifs were created with FITS liberator on windows.
Any idea what's happening here?
I want to paste an image into a single coloured background using PIL but some blures and noises appear around pasted photo like this:
(Zoom photo to see noises. I think it is due from Antialiasing) But I want to paste with sharp boundaries like here :
I am using this codes for paste:
my_image.convert('RGBA')
background = Image.new("RGBA", (background_size), background_color)
background.paste( my_image, (coordinates), my_image )
background.save("result.jpg")
What sholud i do for pasting with sharp boundaries?
Thanks.
jpg is a lossy format, so it may blur your image or add noise, in order to save memory. Use a lossless format like png instead:
background.save("result.png")