Jump to content

brightness/contrast manipulation with C


jtcgreyfox
 Share

Recommended Posts

I got inspired by the webcam laser range finder idea from a few episodes ago, and decided to make my own independently with the only language I am ok at, C.

To start with, i thought about how to make the images at all useful. Having taken some webcam snapshots, i quickly realized the red dot from the laser pointer isnt all THAT bright. So i messed around with brightness and contrast of the images. Resulting in a black and white image, maxed out contrast, and very high brightness, to create an image with a white dot where the laser is, black everywhere else (bar white from ceiling lights)

My problem is, I want to be able to alter the contrast of the webcam capture using C, so I would be able to edit the contrast of each image as it is captured from dorgem, resave it, and THEN work on the maths of the laser range.

Looking at the file structure of image files. I figured .bmp is the best for its simplicity. Although since it is written to the file backwards, i'm not so sure. This would mean i wouldn't be able to edit the image contrast on the fly, I would need to generate a new image file entirely, and work from the 2nd image, which doesn't satisfy my need for pretty programming.

Thanks.

EDIT:

I have found -

//Converts to a percent
//[0, 1]
New_Value = Old_Value / 255

//Centers on 0 instead of .5
//[-.5, .5]
New_Value -= 0.5

//Adjusts by Contrast_Value
//[-127.5, 127.5], usually [-1, 1]
New_Value *= Contrast_Value

//Re-add .5 (un-center over 0)
//[-127, 128]
New_Value += 0.5

//Re-multiply by 255 (un-convert to percent)
//[-32385, 32640], usually [0, 255]
New_Value *= 255

//Clamp
[0, 255]
If(New_Value > 255)
   New_Value = 255
If(New_Value < 0)
   New_Value = 0

from http://www.developerfusion.com/article/544...and-contrast/3/

But I still have the problem of HOW to edit the image.

I don't quite understand the use of an array here. And the actual image file wouldn't be simple enough to use that. The above code is C# right? well its close enough to C but if someone could explain how to map a .bmp to a 2d array that would be a cool way to solve the problem too. But i really want to avoid generating a 2nd image if i can.

Link to comment
Share on other sites

I got inspired by the webcam laser range finder idea from a few episodes ago, and decided to make my own independently with the only language I am ok at, C.

...

But I still have the problem of HOW to edit the image.

Instead of writing all the image handing code yourself, you can save a lot of time by using an imaging library. What compiler are you using?

I often work with Borland C++ Builder, and I use HiComponents' ImageEn library, it is excellent.

I have worked a bit using ImageMagick MagickCore, it's very powerful and open source. (used with GCC)

An imaging library will handle opening/saving whatever file format you want and many imaging libraries have included functions for adjusting brightness, etc, which you may also find useful.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...