Munawwar / codepasta-comments

1 stars 0 forks source link

2019/04/26/background-segmentation-removal-with-opencv-take-2.html #1

Open Munawwar opened 1 year ago

Munawwar commented 1 year ago

Written on 04/27/2019 07:40:37

URL: https://www.codepasta.com/2019/04/26/background-segmentation-removal-with-opencv-take-2.html

Munawwar commented 1 year ago

Comment written by Tejal Mehta on 04/29/2019 13:38:48

Hello, Great tutorial, awesome
you haven't defined "parentID" and used it so throws error

Munawwar commented 1 year ago

Comment written by Munawwar on 04/29/2019 17:23:40

Ah, right. parentId should be -1 in this case. Also it should be hierarchy[0]. Also misspelt hierarchy.

Munawwar commented 1 year ago

Comment written by Victor Abreu on 05/01/2019 16:18:30

Really good tutorial! It helped me segment my images nicely, it works very well one quite many of them.

I had to make a few modifications, the most problematic one is this one:
largestContour = contoursWithArea[0][0] which returns the smallest contour instead of the largest, I had to change that line to largestContour = contoursWithArea[-1][0]

Munawwar commented 1 year ago

Comment written by Munawwar on 05/01/2019 17:23:23

ah yes. forgot to reverse the sort. funny that I never had a problem so far

Munawwar commented 1 year ago

Comment written by Munawwar on 05/01/2019 17:25:51

what other modifications you had to do?

Munawwar commented 1 year ago

Comment written by Munawwar on 05/01/2019 18:37:37

oh I didn't have a problem so far is because I had additional logic to filter out contours with area less than 2% the area of the image. this was from my last tutorial. which btw helps if you have multiple objects to segment.
fixed the code.

Munawwar commented 1 year ago

Comment written by Thomas Dussaut on 05/07/2019 07:29:32

In the function `findSignificantCountours` with OpenCV 4, you have to modify the first line, cause the image isn't returned anymore :

```python
contours, hierarchy = cv2.findContours(
edgeImg,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE
)
```

However I still have a problem when I try to launch the first `grabCut`.

Here is my error :
```
cv2.grabCut(src, trimap, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK)
cv2.error: OpenCV(4.1.0) ..\modules\imgproc\src\grabcut.cpp:382: error: (-215:Assertion failed) !bgdSamples.empty() && !fgdSamples.empty() in function 'initGMMs'
```

I don't know from where it could come :/ Any ideas

Munawwar commented 1 year ago

Comment written by Munawwar on 05/07/2019 07:37:07

No idea. OpenCV 4 is very new right now.. why don't your try 3?

Munawwar commented 1 year ago

Comment written by Thomas Dussaut on 05/07/2019 07:38:38

i'll try with the newest version of the 3, I'll give you some news when i'm done :)

Munawwar commented 1 year ago

Comment written by Thomas Dussaut on 05/07/2019 07:48:17

It fixed the problem :)

Munawwar commented 1 year ago

Comment written by Venus Bhushan on 05/29/2019 06:03:25

C:\Python27\python.exe C:/Users/shashisanha/PycharmProjects/untitled/self.py
Traceback (most recent call last):
File "C:/Users/shashisanha/PycharmProjects/untitled/self.py", line 7, in <module>
edgeDetector = cv2.ximgproc.createStructuredEdgeDetection("model.yml")
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv_contrib\modules\ximgproc\src\structured_edge_detection.cpp:432: error: (-215:Assertion failed) modelFile.isOpened() in function 'cv::ximgproc::StructuredEdgeDetectionImpl::StructuredEdgeDetectionImpl'

I am very new to python as well as image processing so pls forgive me XD

Munawwar commented 1 year ago

Comment written by Munawwar on 05/29/2019 06:31:11

Hi. First, it is better to use opencv 3 than 4 to follow this tutorial (I haven't tested it on opencv 4). And also python 3.
And check if model.yml is in the same directory.

Munawwar commented 1 year ago

Comment written by Venus Bhushan on 05/29/2019 08:02:49

i am also unable to install contrib modules

Munawwar commented 1 year ago

Comment written by DisqusMan on 06/24/2019 06:46:22

Hi,
very thanks, your post has given me a lot of help.
If I want to extract the portraits in the photos, How should I achieve it?

https://uploads.disquscdn.c... https://uploads.disquscdn.c...

Munawwar commented 1 year ago

Comment written by Munawwar on 08/03/2019 16:20:19

You wont be able to use findContour if the part you are trying to extract is touching the image boundaries or not contained within the image boundaries.
I don't have a solution for your problem.. but however for grabcut algorithm to work you don't necessarily need to create trimap from contours. You might be able to use watershed or color clustering or simply some guesses on which parts are sure facial areas/sure clothes/sure background and mark the rest as unsure.

Munawwar commented 1 year ago

Comment written by Munawwar on 08/28/2019 16:39:21

You will struggle with complex background with this method. As mentioned in my 2016 blog post:
"let me first mention the several limitations of this approach:

- contour detection would work poorly if object edges are touching the edges of the image.
- contour detection would work poorly if there are multiple objects with edges touching each other (like for example if one partly covers an object by placing another object on top) or if background texture is very coarse.
- If background color is similar to object color, the edges probably won’t be detected well."

You should try neural net based segmentation to deal with complex backgrounds. Or make an interactive user interface for manually marking grabcut regions. Or use a service like https://remove.bg.

Munawwar commented 1 year ago

Comment written by Nitya on 08/29/2019 05:55:37

Is remove bg payable? Do you have any idea of implementation which describes the neural net based segmentation to deal with complex backgrounds as u mentioned above in java / python? Please send in case

Munawwar commented 1 year ago

Comment written by Yashaswini S on 09/21/2019 15:54:17

I'm getting the same error even if I use opencv 3. How do I resolve it?

Munawwar commented 1 year ago

Comment written by Rebekka on 10/01/2019 15:20:41

Thank you for such a great example. It works like a charm.
Also it would be great if you left some links to additional materials or articles about image segmentation

Munawwar commented 1 year ago

Comment written by Shinichi Kudo on 08/11/2020 11:02:39

OpenCV(3.4.5) /io/opencv_contrib/modules/ximgproc/src/structured_edge_detection.cpp:432: error: (-215:Assertion failed) modelFile.isOpened() in function 'StructuredEdgeDetectionImpl'

File "/home/kudo/Documents/Color dominant/test5.py", line 7, in <module>
edgeDetector = cv2.ximgproc.createStructuredEdgeDetection("model.yml")

This isthe error I got when running this code. Can you help me to solve it?

Munawwar commented 1 year ago

Note to readers: The mess you see above is because I migrated all the comments from Disqus comment system to Github issues + utteranc.es.