Isolate image shadow with OpenCV C++ [closed]

asked 2018-02-21 22:11:41 -0500

por.aca gravatar image

Hi! I have seen some algorithms on how to remove a shadow from an image using OpenCV with C++. I have looked around but haven't find the way to not just erase the shadow, but store it on a new image alone. What I am doing with this code is to convert the original image (that I obtained from the Internet) to the HSV color space, change the value of V=180, which somehow removes the shadow (and changes the coloration as well), and then converting the image back to the BGR color space. I am clueless on how to 'extract' the removed shadow and save it to a different image or channel. Please advice...

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>

using namespace cv;
using namespace std;

int main()
{
    Mat srcImg;
    Mat hsvImg;
    Mat bgrImg;

    srcImg = imread("pcb-2008.jpg");
    cvtColor(srcImg, hsvImg, CV_BGR2HSV);
    imwrite("1.hsv.jpg", hsvImg);

    Mat channel[3];
    split(hsvImg, channel);
    channel[2] = Mat(hsvImg.rows, hsvImg.cols, CV_8UC1, 180);
    merge(channel, 3, hsvImg);
    imwrite("2.hsvNoShadow.jpg", hsvImg);

    cvtColor(hsvImg, bgrImg, CV_HSV2BGR);
    imwrite("3.backToBgr.jpg", bgrImg);

    return 0;
 }

Sample picture of a PCB

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-29 00:08:45.342120

Comments

Using this method, the V channel (in your code it's channel[2] before modification) is considered "shadow". But this is a very simplistic approach (if you display this channel, you'll see why).

You might consider looking at this topic for more sophisticated algorithms.

kbarni gravatar imagekbarni ( 2018-02-22 09:38:26 -0500 )edit

Thank you @kbarni for pointing this link, I will take a look...

por.aca gravatar imagepor.aca ( 2018-02-22 14:28:44 -0500 )edit