1

So I looked a here and there and it seems like it's a common problem. Though, I have a specific need that wasn't solved in the existing threads.

The main idea of my school project is to simulate a tattoo on your skin. For that, OpenCV detects the arm thanks to the skin color.

Thanks to the cv2.getRotationMatrix2D and cv2.warpAffine, I can rotate the tatoo with the arm.

#areaAngle is the inclination in degrees of the region of interest

N = cv2.getRotationMatrix2D((tatWidth/2,tatHeight/2),areaAngle,1)
#imgTatoo is the mustache image
roTatoo=cv2.warpAffine(imgTatoo,N,(tatWidth,tatHeight))

But my problem is this one : When the arm is straight, everything is fine (image) While when I tilt the arm, a magnificent black box appears (image again).

One of the proposed solutions was to crop the image using "bigger rectangle in the area". The thing is i want to keep the full tattoo and not just a cropped part.

Any idea how to do that? Thanks

EDIT : I tried to resize the mask to match the diagonal height but the problem is that because of these lines of code:

tatoo=cv2.resize(imgTatoo,(tatWidth,tatHeight),interpolation=cv2.INTER_AREA)
mask2=cv2.resize(tatMask,(tatWidth,tatHeight),interpolation=cv2.INTER_AREA)
mask2inv=cv2.resize(invTatMask,(tatWidth,tatHeight),interpolation=cv2.INTER_AREA)

and further away

        #create a ROI mask
        roi = frame[fy1:fy2,fx1:fx2]

        # print(roi.shape)

        #merge the roi mask with the tatoo and the inverted tatoo masks
        roi_bg = cv2.bitwise_and(roi,roi,mask = mask2inv)
        roi_fg = cv2.bitwise_and(tatoo,tatoo,mask = mask2)

        # print(roi_bg.shape,roi_fg.shape)

        #merge the background and foreground ROI masks
        dst = cv2.add(roi_fg,roi_bg)

if I try to resize the mask, i have to resize the tattoo image since the arrays need to be the same size.

  • It would help to see a [mcve], so we don't need to guess about what your code looks like. You're probably not taking advantage of `borderMode` and `borderValue` params of `warpAffine`, and you're forgetting that the diagonals are longer than the sides of a rectangle, so the bounding box to fit the result of rotation will be larger than original. You can calculate the required size using simple trig, and add an appropriate amount of padding before calling `warpAffine`. – Dan Mašek Apr 11 '18 at 18:12
  • 1
    To get rid of cropping, you can use some trig (if you're only doing rotation). If you need to work on full affine warps or homographies, see my answer [here](https://stackoverflow.com/questions/44457064/displaying-stitched-images-together-without-cutoff-using-warpaffine) (also has links to a GitHub repo with functions to warp without crop). Dan's suggestion above is perfect for the borders. – alkasm Apr 11 '18 at 18:13
  • I added some code to the initial issue, hope it's clearer. And i checked on the openCV doc for the us of borderMode but it's not very clear... – TouchTheFishy Apr 16 '18 at 13:10

0 Answers0