Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert image black to white and vice versa

I have a jpg picture which is black and white and I would like to convert black part to white and white to black( black pixel to white and vise versa ) in MATLAB and save it as jpg file again. I have tried this code but it just give me a black line in the white page.

 im=imread('Export0000009111.jpg');
 binstring = dec2bin(im, 8);

 binImage = ~binstring;
 binImage = 1-binImage;
 binImage = (binImage == 0);
 imwrite(binImage,'ss1.png');

Does anyone has any proper solution for that?

Thanks in advance!

like image 434
Sam Avatar asked Dec 25 '22 12:12

Sam


1 Answers

Code -

PATHNAME = 'Random.jpg'; %// Original image file
PATHNAME1 = 'RandomModified.jpg'; %// Modified image file

imwrite(uint8(255 - imread(PATHNAME)),PATHNAME1)
figure, imshow(imread(PATHNAME1))

When you read images, normally they come in 2D or 3D matrices with values in between 0 and 255, with 0 being black and 255 being white. So, we only need to subtract every pixel value from 255. This will do your job, will create negative images for gray images and for color image would be give a sense of "colored-negative" if I could invent a term like that.

like image 102
Divakar Avatar answered Dec 29 '22 00:12

Divakar