Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1065817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:54:32+00:00 2026-05-16T19:54:32+00:00

I want to subtract two images. My problem ist that the cvSub()-function saturates. What

  • 0

I want to subtract two images. My problem ist that the cvSub()-function saturates. What I want to do is:

1) Convert the original images to grayscale.

2) Take the grayscale-images (values from 0-255).

3) Subtract the images (values from -255 to 255) -> problem of rescaling using cvSub().

4) Rescale by multiplying with 0.5 and adding 128.

I thought of changing the gray-scale images from 8bit to 16bit, but then everything got worse and it became many lines of code and in the end it didn’t work out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T19:54:33+00:00Added an answer on May 16, 2026 at 7:54 pm

    Using openCV you could do the following:

    • scale both source images (1 and 2) by factor 0.5. Both images are now in range [0..127]
    • shift image 1 by 128. It now is in the range [128..255]
    • subtract image 2 from image 1

    This way, no range conversion is needed and the result is fully scaled to 8 bit. Use the cvConvertScale for the first two operations.

    Something like this:

    //...
    cvConvertScale(src1, tmp1, 0.5, 128);
    cvConvertScale(src2, tmp2, 0.5, 0);
    cvSub(tmp1, tmp2, dst);
    

    EDIT:

    To your comment on loosing information(precision), you are right, but you always do when dividing using integer math. And scaling in your case is just that. Simply think of it as shifting all the bits to the right by one place. So the last bit of information is lost.

    On the other hand, the order of the applied operations is also important. By dividing by 2 you introduce a rounding (or truncation) error of 0.5 for every pixel. If you scale both input images before subtracting them, the rounding error adds up to 1.0. This shows up in the result image as some pixels being off by 1 compared to the result you would get with your initial and Alexanders approach. But that is the tradeoff for the simpler solution without expanding the image to 16-bit or floating point.

    See this example:

    real numbers:
    (200 – 101) / 2 = 99 / 2 = 49.5

    Alexanders solution (integer math):
    (200 – 101) / 2 = 99 /2 = 49

    my Solution (integer math):
    (200 / 2) – (101 / 2) = 100 – 50 = 50

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a function, subtract, that will take two arrays, and return all of
I want to subtract between two date time values using SQL in MySQL such
I want to get the minimum values from two mappings and subtract one from
What I want to do is take my current array and subtract the previous
I have two arrays which contain objects of assets, now I want to subtract
I have two floats in Python that I'd like to subtract, i.e. v1 =
I want to subtract two signed 8bit integers in JavaScript. As JavaScript doesn't have
I want to subtract two time periods say 16:00:00 from 19:00:00. Is there any
I have two unix timestamps as LONG INT. I want to subtract start from
so in my app I want to subtract two dates. The first date is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.