Ask Your Question
2

How to remove black/shadows regions of colored Image via OpenCV

asked 2015-12-04 13:23:42 -0600

VeTaLio gravatar image

updated 2020-11-02 11:37:10 -0600

Hello guys.I want to remove black/shadows regions of coloured image and after this improve contrast and brightness.
So how can i achieve this one?
Can i use equalizeHist or AdaptiveTreshold and after that,convert back to Coloured Image ?
Or what technique i can use to achieve my goal?
Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
4

answered 2015-12-05 06:16:35 -0600

pklab gravatar image

updated 2015-12-11 06:48:16 -0600

Do you want to cut (collapse to black) dark regions or remove (restore image) shadows ?

Shadow removal is a complex task. Use "shadow" in the search box here to read about this subject .

I don't know if @thienlamnhan 's code for detect shadow works. In addiction read this:

Use histEqualization to enhance bright vs dark region.

To collapse dark regions to full black you could use same principle of brightness and contrast normalization

cvtColor(src, gray, CV_BGR2GRAY);
// get full available range
double minGray,maxGray;
cv::minMaxLoc(gray, &minGray, &maxGray);
//suppose current range is 30...220

bool useRelative = true;
if(useRelative)
{
    // Relative clipping dark range to black
    double clipPercent = 10; 
    minGray = cvRound(minGray * (1 + clipPercent/100.0) );
    //all below minGray+10% will become black
}
else
{
    //absolute clipping. Use a fixed lower bound for dark regions
    double minGrayWanted = 50; 
    minGray = minGrayWanted;
    //all below 50 will become black
}

// current range
float inputRange = maxGray - minGray;

alpha = 255.0 / inputRange; // alpha expands current range. MaxGray will be 255
beta = -minGray * alpha;    // beta shifts current range so that minGray will go to 0

src.convertTo(dst, -1, alpha, beta);

Result will be:

src[0...minGray] -> dst[0]
src[minGray...maxGray] -> src[0...255]

[EDIT] from user comment It seems that needs is to correct nonuniform illumination. In this case MORPH_TOPHAT can do the job. See my answer on this subject.

edit flag offensive delete link more

Comments

@pklab hi,thanks for the reply. Yes,i want to "remove (restore image) shadows"(that means,the black/shadows regions must be more brighter.

VeTaLio gravatar imageVeTaLio ( 2015-12-07 02:33:30 -0600 )edit

@pklab i tried and my image converting to full black. Whats wrong? any suggestions?

VeTaLio gravatar imageVeTaLio ( 2015-12-07 03:30:47 -0600 )edit

Using my simple code you can have more brightness or more darkness for the full image not just for shadowed area. I you want to adjust brightness only for shadowed area you have to use some of the techniques described above and/or others anyone would suggest

pklab gravatar imagepklab ( 2015-12-07 11:03:32 -0600 )edit

@pklab yes,but when i tried to implement this in java,my result was -> fully black image :). The problem is,that function minMaxLoc(java version) on input,have parametr: 1) Mat; 2) second variant of method have two parametrs on input : Mat,Mat. And how to get minGray value?(Because its zero by default).

VeTaLio gravatar imageVeTaLio ( 2015-12-08 02:51:36 -0600 )edit

read the doc

Core.MinMaxLocResult mmr = Core.minMaxLoc(img);
minGray = mmr.minVal;
pklab gravatar imagepklab ( 2015-12-08 09:11:41 -0600 )edit

@pklab thanks you so much! So i tried your algorithm and i have strange result. My examples :
source Image -> result
So you can see,that dark regions remains "dark"(but with little bit improved light) and lighter regions are getting more brightness. Thanks!

VeTaLio gravatar imageVeTaLio ( 2015-12-09 04:55:26 -0600 )edit

@pklab also,how you think,this histogram equalization for coloured images is good idea to improve light and contrast?

VeTaLio gravatar imageVeTaLio ( 2015-12-09 06:32:17 -0600 )edit

check EDIT in my answer

pklab gravatar imagepklab ( 2015-12-11 06:46:24 -0600 )edit

@pklab thank you so much! ill try to implement this(nonuniform illumination). Also i would like to know your opinion,about "Gamma correction",is that good choice to improve dark regions?

VeTaLio gravatar imageVeTaLio ( 2015-12-11 06:54:16 -0600 )edit

You can find the the implementation for non uniform illumination correction at the suggested link. There is result using your source image .

To know about Gamma check this.

Please if answer it's fine for you accept it or refine your question about same subject. Open new question for other subject.

pklab gravatar imagepklab ( 2015-12-12 12:58:56 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-04 13:23:42 -0600

Seen: 21,107 times

Last updated: Dec 11 '15