I have a large binary image (4k x 7k pix) from which I want to extract the entire yellow portion as a single rectangle. I tried binary erosion to even out features inside the yellow region. Then I used the bbox
method of skimage.regionprops
but it does not seem to work fast enough for large image with one large bbox. Do you have any suggestion?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Because you are looking for a single bounding box, don't use
regionprops
or any per-object function. This also makes it so that you don't need to try to make a single object out of all the yellow dots.The simplest solution here is to walk over the image, and for each pixel, determine if it is "yellow enough" (whatever that means for your application). If so, add the pixel's coordinates to the running bounding box calculation.
The bounding box calculation is quite simple:
There might be a way with skimage to do this without loops, but I don't know that package at all.
As the image you provided includes distracting axes, and is the wrong colour and too small, I created as realistic a version as I could with ImageMagick like this in Terminal:
The full-size version is 4200x7200.
I then wrote a
numpy
-based version ofbbox
as followsThat runs in 5.3ms on my Mac. Just for fun, I threaded it and ran the horizontal projection and vertical projection on separate parallel threads and it came down to 3.6ms with the same results.
The identified box looks likes this: