Why does this pbm image behaves weirdly - image

I am trying to understand why I am not getting expected result from the following lines of code:
pix=np.asarray(Image.open(File))) #I am reading a pbm file into memory
img = Image.fromarray((pix), '1') #rewriting
img.save("test1.pbm")
newpix=~pix #inverting the image
img = Image.fromarray((newpix), '1')
img.save("test2.pbm")
original image and test1.pbm(image 1) is same, but test2.pbm (image 2) isn't what I am expecting (the foreground pixels become background ones and vice versa). I am attaching the images here (converted to jpeg). What am I doing wrong?
Another issue is that for most of the foreground pixels in test1.pbm, the value is False. But that is not reflected in the saved image.
I converted both of these images from this original image http://www.mathgoodies.com/lessons/graphs/images/line_example1.jpg using Imagemagick.

I don't recognise which language you are using, but your original image, when converted with ImageMagick like this:
convert cdLTY.jpg -negate out.jpg
looks like this:
So I deduce the problem is in your inversion. I don't know what
newpix=~pix
does (probably complelementing it or inverting all the bits?) but I think you need to subtract each pixel from 255 to invert your image, so if a pixel is 10 in the original image, it needs to be 255-10 or 245 in the new image.
Explanation
Pixels are normally encoded with 0=black and 255=white. So, if your pixel was originally black (0) when you do new pixel = 255 - original value, it will become 255-0, or 255 meaning it is now white. Likewise, if a pixel starts off white (255), when you do 255-255 you get 0 which is now black.

Related

How to open a raw YCbCr image file in MatLab?

I am trying to open a 1920x1080 YCbCr raw image file in Matlab, however I am having difficulties getting the following code below to work:
fid = fopen(filePath);
image = fread(fid, 1920*1080, 'uint8=>uint8');
fclose(fid);
image = reshape(image, 1080, 1920);
However, when I go to show the image, it does not look as expected.
The actual image should be colour, however I get a strange black and white image, not resembling the expected output at all.
I have also tried loading it into a 3D array, with each dimension representing one of the Y, Cb and Cr channels, however this also produced a similar output as described before.
Any help would be appreciated.
Ignore this bit and look below at the EDIT:
I don't understand why you are using fread? Why not use imread,
which is mean't for reading images? Using this infamous original
image, as a base for my test script, I could display a YCbCr
image, as shown in the little script below.
original = imread("lenna.jpg");
% figure, imshow(original); % if you want to see how the original image looks
YCbCr_version = rgb2ycbcr(original);
% figure, imshow(YCbCr_version); % if you want to see how the YCbCr image looks
imwrite(YCbCr_version, "out.jpg");
YCbCr_fromFile = imread("out.jpg");
figure, imshow(YCbCr_fromFile);
EDIT:
IF however you have a binary version of the file and can only read it using using fread,
then the following script should work,
clc;
clear;
close all;
original = imread("lenna.jpg");
% figure, imshow(original); % if you want to see how the original image looks
YCbCr_version = rgb2ycbcr(original);
% figure, imshow(YCbCr_version); % if you want to see how the YCbCr image looks
fileID = fopen('out.bin','w');
fwrite(fileID, YCbCr_version, 'uint8');
fclose(fileID);
fileID = fopen('out.bin','r');
fromFile = fread(fileID, 512*600*3, 'uint8=>uint8');
fclose(fileID);
image = reshape(fromFile, 512, 600, 3);
imagesc(image)
The point is, in the read operation, you have to give the 3 channels in the multiplier also, as color images have this 3rd dimension, i.e. 512*600*3. If you only give 512*600, as you were doing, you would have no color info. Also the reshape function would need to be changed to take into account the 3rd dimension. Hence, reshape(fromFile, 512,600, 3).
YCbCr version loaded from the file
As you said "Any help would be appreciated", I thought I would mention you can simply convert a raw YCbCr file to PNG, TIFF, JPEG or any other format file with ImageMagick which is installed on most Linux distros and is available for macOS and Windows.
Start a Terminal, (or Command Prompt if under Windows), and convert the YCbCr image.raw to PNG with:
magick -size 1920x1080 -depth 8 YCbCr:image.raw result.png
Or, say a CCIR 601 YUV file to NetPBM PPM format:
magick -size 800x600 -depth 8 YUV:image.raw result.ppm

How do I make a png with transparency appear transparent in MatLab?

I have an image with a transparent background, but when I open it in MATLAB, I get a black background. I'm overlaying it on top of a background image. How can I get this to display? I've tried using the alpha function alpha(image,0) but it sets my entire image to 0. Is it possible for me to set the alpha of individual pixels to be equal to 0? That way I can run each pixel through a loop.
I'm not sure if this helps, but when I run a imfinfo('ryu1.png'), I get :
...
Transparency = 'alpha'
SimpleTransparencyData = []
...
You can read in your image using imread. However, you need to specify additional output parameters if you want to grab the alpha channel. You need to call it like this:
[im, map, alpha] = imread('ryu1.png');
im is your image read in, map is the colour map which we will ignore, but alpha contains the transparency information that you want. First call imshow and record the handle to the image, then set your transparency with the alpha channel using the set command. In other words:
[im, map, alpha] = imread('ryu1.png');
f = imshow(im);
set(f, 'AlphaData', alpha);
This should make the figure with the transparency intact.
Addition (Thanks to Yvon)
Supposing you already have a background image loaded into MATLAB. If you want to blend these two together, you need to do some alpha matting. You use the alpha channel and mix the two together. In other words, supposing your background image is stored in img_background and img_overlay is the image you want to go on top of the background, do this:
alphaMask = im2double(alpha); %// To make between 0 and 1
img_composite = im2uint8(double(img_background).*(1-alphaMask) + double(img_overlay).*alphaMask);
The first step is necessary as the alpha map that is loaded in will be the same type as the input image, which is usually uint8. We need to convert this to a double image such that it goes in between 0 and 1, and im2double is perfect to do this. The second line converts each of the images to double precision so that we can compute this sum and in order to make the data types between the alpha mask and both images are compatible. We then convert back to uint8. You can then show this final image using imshow.

PyGame Poor Image Quality & Gradient Banding

I noticed that when displaying jpg or png images they look alot like a GIF file in that there is limited colors and "banding".
You can see the original and a screenshot attached. Kinda hard to tell scaled down but you can see it.
Actually better example. See the banding around the circle?
Here is my code:
#pygame code to render an image
import pygame, os
import time
image = 'gradient-test.png' #located in same folder as this file:resized in Photoshop
pygame.init() #I assume you did this?
SCREEN = pygame.display.set_mode((1366, 768))
pygame.mouse.set_pos((1366, 768))
picture = pygame.image.load(image)
SCREEN.blit(picture,(0,0))
pygame.display.update()
time.sleep(5)
It seems like a problem with the pixel format of your surface. You can add the following lines to your script to see if there's a difference between the pixel format of your image surface and your screen surface:
print 'picture', picture.get_bitsize()
print 'screen', SCREEN.get_bitsize()
It's good practice and recommended to always change the pixel format of any new surface to the pixel format of your screen surface by calling convert():
convert()
change the pixel format of an image
convert(Surface) -> Surface
Creates a new copy of the Surface with the pixel format changed ...
If no arguments are passed the new Surface will have the same pixel format as the display Surface. This is always the fastest format for blitting. It is a good idea to convert all Surfaces before they are blitted many times.
It's simple:
picture = pygame.image.load(image).convert() # added convert() call
Also, you can try to set the color depth of your screen manually, like:
SCREEN = pygame.display.set_mode((1366, 768),0, 32) # use 32-bit color depth

How to Save an image in matlab

I use the below command to display the image
imshow(img,[]);
when i use the following command to save the image it is saved as an empty white page
imsave;
how to save the image in this case any command would do
You are probably running into an issue with matrix type and range. If img is type double it needs to be scaled between 0 and 1.
A common issue is to load an image in uint8 (scaled between 0 and 255), convert to double in order to do some processing on it, without scaling, and then try and save it out. When you do that, MATLAB tries to convert back to uint8, and any values in the image outside the [0 1] range are clipped. On many images this means that the file comes out all white.
To get around this, use functions like im2double and im2uint8 rather than just double or uint8 when converting images.
Try at the command line the difference between:
img = imread('pout.tif');
img = double(img);
imshow(img,[]);
imsave;
and
img = imread('pout.tif');
img = im2double(img);
imshow(img,[]);
imsave;
Convert image data into an actual image and try again:
h = image(img); %Convert to object
imsave(h); %Save image object
Notice that if you close the figure window generated by image(), the object is deleted and the handle has will point to nothing. Though this may be beyond of what you are asking for.
Hope this adjustment solved your problem
First convert the image to rgb using
img1=label2rgb(img);
then again convert the image into an gray image using
img2=rgb2gray(img1);
then u can use imshow to show the image and save it using imsave
imshow(img2);
imsave();

OpenCV2.3 imwrite saves black image

I am trying to save a JPEG image onto the disk using imwrite, seems that I am missing something. I am always getting a black image of around 4KBs. What am I doing wrong here?
Image I see seems fine but once onto the disk, its completely black.
std::vector<int> qualityType(1);
qualityType.push_back(CV_IMWRITE_JPEG_QUALITY);
cv::imwrite("Final.jpg",image,qualityType);
The following code works for me on 8bit (1 and 3 channel) images:
std::vector<int> qualityType;
qualityType.push_back(CV_IMWRITE_JPEG_QUALITY);
qualityType.push_back(90);
cv::imwrite("Final.jpg",image,qualityType);
In your code qualityType is initialized incorrectly. Your vector contains 2 values
{<some unknown number>, CV_IMWRITE_JPEG_QUALITY}
but should be
{CV_IMWRITE_JPEG_QUALITY, <desired quality value>}
imwrite prints on a 0 to 255 scale, but your image is in a 0 to 1 scale. To scale up, use this line:
image.convertTo(image, CV_8UC3, 255.0);
I only had to convert it to 16bit image
image.convertTo(image,CV_16UC3,255,255);
as per document, 8 or 16 bit images can be saved.

Resources