Do we have a function to invert gray image values?

hi :slight_smile:

Do we have a function to invert gray image values? [ 0->1 , 1->0 or for bgr image 50->205, 205->50 ]
in emgucv i use like this : MaskImageMat_InfoFlow.ToImage<Bgr, byte>().Not() but in opencv i donā€™t know how do this.


k i found answer [for gray or bgr img], i msut use this function : cv::bitwise_not(MaskImageMat_InfoFlow, MaskImageMat_InfoFlow_Inverted);

that is bitwise negation.

that gives different results from subtraction.

you should just perform subtraction, like 255 - image

1 Like

yes ty, i tested it now, These two methods have the same result for the gray image but different results for the bgr image.
(1) bitwise_not
(2) 255 - img

try cv::Scalar(255, 255, 255) - ...

itā€™s interpreting 255 as cv::Scalar(255), which is ā€œbroadcastā€ to be cv::Scalar(255, 0, 0), which is why the picture is blue, and all other colors go to black.

1 Like

ty, I tested again, these 2 methods give me equal results for gray and bgr input image
(1) bitwise_not
(2) Scalar(255, 255, 255)- img

// Image input is bgr

// Image input is gray

cv::Mat img1 = ReadImageFromUserInput("Insert Input Image Path : ", cv::IMREAD_GRAYSCALE);
cv::imshow("(1)", img1);

cv::Mat img2 = cv::Scalar(255, 255, 255) - img1;
cv::imshow("(2)", img2);

cv::Mat img3;
cv::bitwise_not(img1, img3);
cv::imshow("(3)", img3);
1 Like

do not use the BGR or RBG color space, you need to convert to the HSV color space and alter the V brightness vector v = 255 -v

did you even try that?

look at the catā€™s chest, or any other dark region. thatā€™s what happens when your method inverts blackā€¦ arbitrary colors, but light instead of dark.

>>> im = cv.imread("cat.jpg")
>>> hsv = cv.cvtColor(im, cv.COLOR_BGR2HSV)
>>> hsv[:,:,2] = 255 - hsv[:,:,2]
>>> cv.imshow("wat", cv.cvtColor(hsv, cv.COLOR_HSV2BGR)); cv.waitKey()

output:

source:

Wasnā€™t v a value between 0 and 100? I might be wrong

8-bit images: Vā†255V,Sā†255S,Hā†H/2(to fit to 0 to 255)
see OpenCV: Color conversions