I have generated images from UE4 using UnrealCV and each image has RGB as well as mask format. Every image contains only a single object and background also present. Most of the time object shape is rectangular/square. I want to crop as well as have the location of the object only. I know if I get the location using contour then cropping is easy. I have also done it for some object. But the problem is for every object it is not working. Sometimes it is saying that no contour has detected, sometimes detecting the wrong contour. My question is, is there any ground truth which will be a solution for every image of mine?
Till now I have done converting BGR2GRAY and then done the thresholding(adaptive thresholding also done) and then try to find the contour. I have also tried to do the steps after converting BGR2HSV but every time failed. I am giving the code that I have tried
import numpy as np
import cv2
import os
rgb=cv2.imread('image_path/rgb_1.png')
mask = cv2.imread('image_path/mask_1.png')
imgray=cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
# ret,thresh = cv2.threshold(imgray,127,255,cv2.THRESH_BINARY_INV)
# thresh = cv2.adaptiveThreshold(imgray,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
# thresh = cv2.adaptiveThreshold(imgray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
#Here I have given all the thresholding way that I have tried
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
if len(contours)>0:
print('here length of contours: ',len(contours))
cnt=contours[0]
x,y,w,h = cv2.boundingRect(cnt)
draw_contour_mask=cv2.rectangle(mask,(x,y),(x+w,y+h),(255,255,255),1) #draw this box on masked image
# cv2.imshow('rec_mask',draw_contour_mask)
draw_contour_rgb=cv2.rectangle(rgb,(x,y),(x+w,y+h),(255,255,255),1) #draw this box on rgb image
# cv2.imshow('rec_rgb_chair',draw_contour_rgb)
crop_rgb = draw_contour_rgb[y:y+h, x:x+w] #to crop the applied bounded box
cv2.imshow("cropped_chair", crop_rgb)
if cv2.waitKey() == ord('q'): #press q to close the output image window
cv2.destroyAllWindows()
else:
print('Now length of contours: ',len(contours))
pass
if cv2.waitKey() == ord('q'): #press q to close the output image window
cv2.destroyAllWindows()
Here [some example images]https://i.stack.imgur.com/WaVR6.jpg has given for your convenience.
If you need further info please let me know. Hope to get a response and solve the issue.