PDA

View Full Version : Subtract QImage



enricong
23rd July 2012, 16:47
I would like to subtract two QImages so the resultant image shows a difference.
I know I can brute-force loop through each pixel and subtract that way.

I was wondering if there is already a built in function that can do this.

tescrin
23rd July 2012, 17:58
First, I don't see anything in the Qpixmap documentation or QIcon documentation that suggests this functionality is in place; but it's worth noting that your problem is not well-defined:

-What happens if you subtract a larger from a smaller number? (I.E. (20,20,20)Pixel - (40,40,40)Pixel)
-What happens between a color without an alpha value vs. one with an alpha value?
(I.E. (20,20,20,255) - (20,20,20))
-Must the images be the same size? If not, what do you do? You could error out, or you could just show the difference between the existing values. Or show an image only modified by the difference of the smaller picture.
-etc..


There are probably a billion very specific cases you'll cover in your own code. Were there functionality in place already you'd probably have to modify it. There are (somewhat) obvious solutions such as:
-Treat "no" alpha value as 255
-Subtract only until you hit zero
-Show the absolute value of the difference between pixels
-etc..

but even so, it's such an odd thing to ask for IMO that it's unlikely two people wanting it would program it the same.


IMO Go with the simple:

for each x pixel
for each y pixel
new_images_pixle(absolute_value(image1.x,image2.x) ,absolute_value(image1.y,image2.y))


and cover all of your goofy issues the way you desire.

enricong
23rd July 2012, 19:21
I've seen this function in various imaging and math software.
The same sized image would need to be used.
Typical the Absolute Value is taken so black would mean no difference.
However sometimes the number is allowed to go negative then it is normalized so grey would mean no difference.

But yes I pretty much went with what you said.