I am using torch with some semantic segmentation algorithms to produce a binary mask of the segmented images. I would then like to crop the images based on that mask. To be clear I need to crop it on a per pixel basis. It seems like a simple problem but the only solution I can conjure up is to either invert a draw mask
function like in the Coco API, or iterate over each pixel in the array and mask together setting the pixel to black if not needed. I feel like there is a better way of doing this. Libraries in Lua, Python, Go, or C++ will work for me. Any ideas?
相关问题
- Views base64 encoded blob in HTML with PHP
- How to get the background from multiple images by
- Try to load image with Highgui.imread (OpenCV + An
- CV2 Image Error: error: (-215:Assertion failed) !s
- Is it a bug of opencv RotatedRect?
相关文章
- Use savefig in Python with string and iterative in
- Where does this quality loss on Images come from?
- Specifying image dimensions in HTML vs CSS for pag
- How to insert pictures into each individual bar in
- How do I append metadata to an image in Matlab?
- Img url to dataurl using JavaScript
- Click an image, get coordinates
- opencv fails to build with ipp support enabled
You can use the
boundingRect
function from opencv to retrieve the rectangle of interest, and you can crop the image to that rectangle. A python implementation would look something like this:substitute
mask
animg
with your ownHere is a solution relying only on numpy:
now execute
get_segment_crop(rgb, mask=segment_mask)
wherergb
is an ndarray of shape (w,h,c) andsegment_mask
is a boolean ndarray (i.e. containing True/False entries) of shape (w,h), given that w=width, h=height.For anyone else running into this. I found good luck with converting the torch binary mask tensor into type
Double
, and then simply multiplying it using torch'scmul
function against each of the RGB channels. Basically, because the binary mask has a1
in place of a segmented pixel, then the value will just remain. Whereas if it is outside the segmentation it has a0
which when multiplied across the channels produces black. Saransh's answer is also good, and works well for open cv.I've implemented this in Python, assuming that you have your input image and mask available as Mat Objects. Given that src1 is your image and src1_mask is your binary mask:
Now mask_out contains the part of the image src1 located inside the binary mask you defined.
Use OpenCV .copyTo with the mask option
http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html#mat-copyto