2

I have some pictures like this: enter image description here

After choosing the contour that I want by index by the function cv2.findContours: enter image description here

I want to remove the nearly white background outside this contour. I tried this method: Removing background color from image opencv python but I don't know how to modify the mask such that the inner area of the contour is white and the outer is back.

Could anyone tell me how? Thanks in advance

Long Do
  • 21
  • 5
  • 1
    Once you have your mask, just invert as follows assuming it is 0 (black) and 255 (white): `mask = 255 - mask` – fmw42 Aug 05 '20 at 04:18
  • But I don't know how to create a mask such that the area inside contour is white and the outer is back. – Long Do Aug 05 '20 at 04:23
  • How does your expected output may look like? – ZdaR Aug 05 '20 at 04:40
  • I expect the output just has the rock in a full-white background – Long Do Aug 05 '20 at 04:43
  • 1
    You said you had a mask image already. Did I misunderstand? If not, use cv2.drawContours() on a black image and set the flag to -1 for the thickness and the color to white and it will be a filled contour with the center white on a black background. Then invert. Or draw a black filled contour on white background image. See https://docs.opencv.org/4.1.1/d6/d6e/group__imgproc__draw.html#ga746c0625f1781f1ffc9056259103edbc – fmw42 Aug 05 '20 at 06:12

0 Answers0