Ask Your Question

Converting transparent pixels to white after mask proccess

asked 2016-10-28 10:00:28 -0500

eblek gravatar image

I m working on optical mark recognition. My purpose is extracting the red circles from the image and evaluating black shapes after. There is no problem on detecting red circles and extracting them (picture 2), i successfully obtain the image where red pixels are transparent. The problem is that, when i convert this image to grey, transparent pixels becomes black, so nothing changes after all this effort(picture3). How can i convert transparent pixels to white or i m making somethings very wrong?

    Mat masked = new Mat(src.size(), CvType.CV_8UC3, new Scalar(255, 255, 255));
    Mat hsv_image = new Mat();
    Imgproc.cvtColor(src, hsv_image, Imgproc.COLOR_RGB2HSV);

    Mat lower_red_hue_range = new Mat();
    Mat upper_red_hue_range = new Mat();
    Core.inRange(hsv_image, new Scalar(0, 70, 70), new Scalar(10, 255, 255), lower_red_hue_range);
    Core.inRange(hsv_image, new Scalar(160, 70, 70), new Scalar(179, 255, 255), upper_red_hue_range);
    Mat red_hue_mask = new Mat();
    Core.addWeighted(lower_red_hue_range, 1.0, upper_red_hue_range, 1.0, 0.0, red_hue_mask);
    Core.bitwise_not(red_hue_mask, red_hue_mask);
    Utils.saveAsBitmap(red_hue_mask);  // picture1

    src.copyTo(masked, red_hue_mask);
    Utils.saveAsBitmap(masked); // picture2

    Imgproc.cvtColor(masked, mBlacked, Imgproc.COLOR_RGB2GRAY);
    Utils.saveAsBitmap(mBlacked); // picture3

image description image description image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2016-10-30 15:22:19 -0500

Tetragramm gravatar image

I don't know why you're having this problem, but I have a couple of suggestions.

First, replace the addWeighted with bitwise_or. It's just more descriptive of what you want done, and quicker. Also it might possibly have a problem when you add 255+255, I dunno.

Second, get rid of the masked variable. Just convert src to gray and then use the setTo method. You can use this with a mask to set every "on" pixel to white. That means you need to do this with a non-negated red_hue_mask, so get rid of the bitwise_not.

Imgproc.cvtColor(src, mBlacked, Imgproc.COLOR_RGB2GRAY);
mBlacked.setTo(new Scalar(255,255,255), red_hue_mask);

That should bypass whatever weirdness is going on with cvtColor. Let me know if that doesn't work.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-10-28 10:00:28 -0500

Seen: 503 times

Last updated: Oct 30 '16