Image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) Here is the code I used when analyzing this image: import cv2ĭef remove_black_border_by_sobel(image, std_factor=3): Note that this method might not work as well when there are "fake margins" that you don't want to be removed. We get graphs with only one point being 3 standard diviations away from the mean.Īverage per column of I_x and per row of I_y for partly cropped image The max/min of the graph is taken as the margin only if it is at least 3 standard deviations larger than the mean (why 3? because of 'the empirical rule' and because it works for most images I tried it with) This is where this method is still dependent on some threshold, but it is a statistical one. What about images with only partial margins? Note that this image has a black margin at all sides (even a 1 pixel margin at the right side, which is hard to notice) This is actually helpful, as we can just take the maximum as the left/top margin and the minimum as the right/bottom one. This is a sum of a directed derivative so the spikes are signed. Next, I summed each column of I_x and each row of I_y.Īverage per column of I_x and per row of I_y Note how bright is the transition between the white part of the flag and the black boarder in the output I_y as it represents the steepest change in greyscale. I used the sobel operator in order to get an approximation. If we calculate the derivatives of the greyscale image with respect to the horizontal and vertical changes, we should expect a spike where the margin meets the image. crop the image using the indices you found.preform validity checks to decide whether to actually output the indices you found in step 3.for each list of sums, find the minimum and maximum.sum I_x along the y axis and I_y along the x axis.use the Sobel operator on the image and get I_x and I_y (derivative of greyscale image w.r.t the axis).I figured out a way that is less dependent on a threshold. # get contours (presumably just one around the nonzero pixels)Ĭontours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)Ĭontours = contours if len(contours) = 2 else contours Thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) Thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel) # apply close and open morphology to fill tiny black and white holes Thresh = cv2.threshold(blur,236,255,cv2.THRESH_BINARY) Gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) Use numpy slicing to crop that area of the image to form the resulting image.Apply morphology to remove small black or white regions then invert again.Here is one way to do that in Python/OpenCV.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |