Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如何将图像从黑色转换为白色,反之亦然_Image_Matlab_Image Processing - Fatal编程技术网

Image 如何将图像从黑色转换为白色,反之亦然

Image 如何将图像从黑色转换为白色,反之亦然,image,matlab,image-processing,Image,Matlab,Image Processing,我有一张jpg图片,它是黑白的,我想在MATLAB中将黑色部分转换为白色,将白色部分转换为黑色(黑色像素转换为白色,反之亦然),并再次将其保存为jpg文件。 我已经尝试过这个代码,但它只是给了我一个在白页黑线 im=imread('Export0000009111.jpg'); binstring = dec2bin(im, 8); binImage = ~binstring; binImage = 1-binImage; binImage = (binImage == 0); i

我有一张jpg图片,它是黑白的,我想在
MATLAB
中将黑色部分转换为白色,将白色部分转换为黑色(黑色像素转换为白色,反之亦然),并再次将其保存为jpg文件。 我已经尝试过这个代码,但它只是给了我一个在白页黑线

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

 binImage = ~binstring;
 binImage = 1-binImage;
 binImage = (binImage == 0);
 imwrite(binImage,'ss1.png');
有没有人有合适的解决办法

提前谢谢

代码-

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

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

当您读取图像时,通常它们以2D或3D矩阵形式出现,值介于0和255之间,0表示黑色,255表示白色。所以,我们只需要从255中减去每个像素值。这将完成你的工作,为灰色图像创建负片,为彩色图像创建负片,如果我能发明这样一个术语的话,会给人一种“彩色负片”的感觉。

要补充另一个答案,如果你想对黑白图像执行二进制操作,你需要先将其转换为二进制图像。因此,如果你这样做:

im=imread('Export0000009111.jpg');
BW = im2bw(im,graythresh(im));
然后,您可以使用您尝试过的方法:

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

我试着用描述和图片把它弄清楚

% read the input image
im = imread('rice.png');

% now convert the image to binary
bin_im = im2bw(im,graythresh(im));

% take complement of binary image
bin_im = imcomplement(bin_im);

% store the image in .jpg format
imwrite(bin_im,'ss1.png');
以下是输出

           input image                  output image

@Divakar,PATHNAME1='Red1.jpg';,我只有一张黑白照片?!!路径名为“Export0000009111.jpg”,路径名1为“ss1.png”。相应地编辑代码。@Lokesh,谢谢,这是一个容易理解的答案。