how to detect blobs and crop them into png files?

喜夏-厌秋 提交于 2019-12-07 09:42:52

问题


i've been working on a webapp. i got stuck here in a problematic issue. i'll try to explain what im trying to do. here you see first big image which has green shapes in it. what i want to do is to crop those shapes into different png files and make their background transparent like the example cropped images below the big one.

The first image will be uploaded by user and i want to crop into pieces like the example cropped images above.it can be done with GD library of php or by a server-side software written in python or c#. but i dunno what this operation called so i dunno what to google to find information. it is something to do with computer vision detecting blobs and cropping them into pieces etc. any keywords,links would be helpful.

thanks for helps


回答1:


Opencv provides a function named cv::findContours to find connected components in an image. If it's always green vs white, You want to cv::split the image into channels, use cv::threshold on the blue or the red channel (those will be white in the white regions and near black in the green region) with THRESH_BINARY_INV (because you want to extract the dark regions), then use cv::findContours to detect the blobs. You can then compute the bounding rectangle with cv::boundingRect, create a new image of that size, and use the contour you got as a mask to fill the new image.

Note: These are links to the C++ documentation, but those functions should be exposed in the python and C# wrappers - see http://www.emgu.com for the latter.




回答2:


A really easy way to do this is to use Flood Fill/Connected Component Labeling. Basically, this would just be using a greedy algorithm by grouping any pixels that were the same or similar in color.

This is definitely not the ideal way to detect blobs and is only going to be effective in limited situations. However, it is much easier to understand and code and might be sufficient for your purposes.




回答3:


I believe this Wikipedia article covers the problem really well: http://en.wikipedia.org/wiki/Blob_detection

Can't remember any ready-to-use solutions though (-:




回答4:


It really depends on what kinds of images you will be processing.

As Brian mentioned, you could use Connected Component Labeling, which usually is applied to binary images, where foreground is denoted by white pixels and background by black pixels (or the opposite). The problem is then how to transform the original image to a binary one. If all images are like the example you provided, this is straightforward and can be accomplished with thresholding. OpenCV provides useful methods:

  • Threshold
  • FindContours for finding contours of connected components
  • DrawContours for extracting each component individually into a separate image

For more complex images, however, all bets are off.



来源:https://stackoverflow.com/questions/4783857/how-to-detect-blobs-and-crop-them-into-png-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!