3

I'm a newbie in image processing.

I'm trying to resize the rectangle/frame bound my object in transparent image.

enter image description here

But i don't know how to make it.

Please help me.

Thank a lot. P/s: It doesn't duplicate with crop. In crop you have fix a tuple (Crop from x, y, w, h). But in my picture, I don't know where to crop. We need to detect minimize rectangle that contain my object(sunglass) first and crop then.

Community
  • 1
  • 1
Jame H
  • 1,324
  • 4
  • 15
  • 26
  • Possible duplicate of [How to crop an image in OpenCV using Python](https://stackoverflow.com/questions/15589517/how-to-crop-an-image-in-opencv-using-python) – api55 Sep 18 '17 at 07:05
  • In the possible duplicate one, you have 2 versions, with PIL and with OpenCV. The only possible difference is the transparence part, that you have to put -1 in the flags part when you load in OpenCV at least. – api55 Sep 18 '17 at 07:07
  • @api55 No It's duplicate with crop. Crop you have fix a tuple (Crop from x, y, w, h). But in my picture, I don't know where to crop. We need to detect minimize rectangle that contain my object(sunglass) – Jame H Sep 18 '17 at 07:35
  • Ahhh, ok, I will retract my close vote then. To get the rectangle you need is to iterate the point and check the alpha value, if it is transparent then continue, if not then compare it to the save values (if it min in x or y or max in x or y), update the values if needed and then build a rect out of it and you are done. I can put it in code in a couple of hours if you want to wait :) – api55 Sep 18 '17 at 07:49
  • @api55: could you give me your code? Thanks – Jame H Sep 18 '17 at 07:55
  • sure, I will post an answer as soon as I am able to – api55 Sep 18 '17 at 08:03

1 Answers1

6

First you have to load the image with alpha support in OpenCV

import cv2
import numpy as np #needed in the second step
im = cv2.imread("image.png", cv2.IMREAD_UNCHANGED)

Notice the cv2.IMREAD_UNCHANGED, this is equal to -1. This will load an image with the format BGRA

Then you find the bounding rect of the object

# axis 0 is the row(y) and axis(x) 1 is the column
y,x = im[:,:,3].nonzero() # get the nonzero alpha coordinates
minx = np.min(x)
miny = np.min(y)
maxx = np.max(x)
maxy = np.max(y) 

Then you crop the object

cropImg = im[miny:maxy, minx:maxx]

Finally you show and save your results to disk

cv2.imwrite("cropped.png", cropImg)
cv2.imshow("cropped", cropImg)
cv2.waitKey(0)

I have no time to test this code, so I may have a typo. I hope it helps you. Any problems, just comment this answer

UPDATE

Here is a small update to remove the extra white part:

First get a boolean mask where it is white

whiteCellsMask = np.logical_and(cropImg[:,:,0] == 255, np.logical_and(cropImg[:,:,1] == 255, cropImg[:,:,2]== 255))

Then change the alpha of the masked values to 0

cropImg[whiteCellsMask,:] = [255, 255, 255, 0]

This will change all the pixels which are white (255,255,255) to transparent (alpha = 0).

api55
  • 11,070
  • 4
  • 41
  • 57
  • This code not crop contour object, please see my picture https://i.stack.imgur.com/Xy9VB.png. Please can remove some white noise boundary of this picture. It looks very bad when I insert this picture to another – Jame H Sep 19 '17 at 04:20
  • @JameH Well, your question was to resize the image to fit the borders, however.... it is possible to do it since the extra part you want to remove is white, look at my updated code.. – api55 Sep 19 '17 at 07:49