如何将图像黑色转换为白色,反之亦然 [英] How to convert image black to white and vice versa

查看:215
本文介绍了如何将图像黑色转换为白色,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张黑白相间的jpg图片,我希望在 MATLAB 并再次将其保存为jpg文件。
我试过这段代码,但它只是在白页中给我一条黑线。

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?

提前致谢!

推荐答案

代码 -

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减去每个像素值。这将完成你的工作,将为灰色图像创建负像,如果我可以发明这样的术语,彩色图像会给人一种彩色负面的感觉。

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.

这篇关于如何将图像黑色转换为白色,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆