필터 지우기
필터 지우기

How to Create function to invert Image colors

조회 수: 157 (최근 30일)
Krish Desai
Krish Desai 2015년 10월 8일
편집: DGM 2022년 11월 26일
I need to write a function that inverts the colors (negative effect) of an image. I know that I need subtract each RGB color value from 255, but I do not know how to write function to do this. The following is my best guess:
% Invert image
s = input('What is the value of the threshhold (from 0 to 255) ');
Inverted = invert(current_img); % create your own function for inverting
%for current_img
current_img(:,:,1)= current_img(:,:,1)-s;
current_img(:,:,2)= current_img(:,:,2)-s;
current_img(:,:,3)= current_img(:,:,3)-s;
  댓글 수: 1
Adam
Adam 2015년 10월 9일
If you are just inverting the colours where does 's' fit in? Your code doesn't do any subtraction from 255 despite you saying that is what you need.

댓글을 달려면 로그인하십시오.

답변 (3개)

Deepanshu Mehta
Deepanshu Mehta 2019년 8월 16일
Or you can simply use
img=imread('image.png');
y=255-Img;
imshow( y );
Thanks me later...
  댓글 수: 2
Aaditya Aaditya
Aaditya Aaditya 2022년 2월 28일
ok, but this is image negation, not inversion, unless image is black & white.
Rik
Rik 2022년 2월 28일
@Aaditya Aaditya Can you explain what should happen instead? I don't see how this wouldn't be an inversion. Looks like one to me.
im=imread('peppers.png');
subplot(1,2,1)
imshow(im)
subplot(1,2,2)
imshow(255-im)

댓글을 달려면 로그인하십시오.


Robert Dylans
Robert Dylans 2015년 10월 9일
A simple invert of image 'x' into image 'y':
x=imread('image.jpg');
y(:,:,:)=255-x(:,:,:);

DGM
DGM 2022년 10월 25일
편집: DGM 2022년 11월 26일
If you can be certain of the class of incoming images, you simply subtract the image from the nominal white value associated with that class. That said, assuming that all images are always uint8 is a great way to cause problems. Unless you know that the incoming images are always uint8, then expect that the nominal white value is not always 255.
As @Robert Dylans points out, Image Processing Toolbox (IPT) has a class-agnostic tool that does this quite safely. If your images are correctly-scaled for their class, you can just use imcomplement().
If you don't have IPT, you can use the attached file from MIMT. It should not be dependent on MIMT or IPT.
% some images of varied class
Auint8 = imread('peppers.png');
Aint16 = im2int16(Auint8);
Adouble = im2double(Auint8);
% invert them all
Buint8 = iminv(Auint8);
Bint16 = iminv(Aint16);
Bdouble = iminv(Adouble);
% create a montage
montage({Auint8 Buint8; Aint16 Bint16; Adouble Bdouble})
Open that file and see how it does the inversion for all the different image classes. It's a bit more succinct than what's been described.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by