Prep_img, contours, hierarchy = prep( img.copy(), kern_size ) # dilate img and check current contour count. # given img & number of desired blobs, returns contours of blobs.ĭef blobbify(img, num_of_labels, kern_size = 3, dilation_rate = 10): Return cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) # Find Contours of Image Retval, img = cv2.threshold(img, 200.0, 255.0, cv2.THRESH_BINARY_INV) # invert colors for findContours Y,x = eroded.shape # get shape of image to make a white boarder around image of 1px, to avoid problems with find contours. Kern = np.ones((kern_size,kern_size),np.uint8) # make a kernel for erosion based on given kernel size.Įroded = cv2.erode(img, kern, 1) # erode your image to blobbify black areas Retval, img = cv2.threshold(img, 254.0, 255.0, cv2.THRESH_BINARY) # threshold to deal with only black and white. # erodes image based on given kernel size (erosion = expands black areas) Then simply find each contours' bounding box and you are done. Now take your image and put it through a while loop that erodes at a set rate that you specify, finding external contours in the image in each loop and stopping the loop once you have the same number of external contours as your num_of_labels. If you have meta data for each image, say in an xml file, that states how many rooms are labeled in each image, then you can access that xml file, get the data about how many labels are in the image, and then store that number in some variable say, num_of_labels. Still, useful to know it exists if you don't want to code the algorithm yourself. So this is a tool to be used, which goes against OP's want for making it from "First Principles", as stated in comments. It is a library that is meant to be used in your applications, not recreate algorithms. Obviously, a wrapper will be needed in order to use this library with python, and at the moment I do not see an official one offered. Here is a c library that implements their algorithm, but it is stated to be very raw and the documentation is stated to be incomplete. You can still use the code above to remove the blueprint design though, and that may help the overall performance of the swt algorithm. To be honest, if this is as fast and reliable as I believe it is, then this method is a more efficient method than my below code. The typical way to find text areas: you can find text regions by using stroke width transform as depicted in "Detecting Text in Natural Scenes with Stroke Width Transform " by Boris Epshtein, Eyal Ofek, and Yonatan Wexler. As stated before, there are different ways of going about this. Now that all you have is the black text the goal is to get those boxes. Return cv2.bitwise_and(gray, gray, mask = mask) Mask = cv2.inRange(gray, lower_bound, upper_bound) Hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # removes pixels in image that are between the range ofĭef remove_gray(img,lower_val,upper_val): # must import if working with opencv in python Doing so will leave only the black text left. I would first white out all content that is not black (or already white). If all of your images you want to edit are roughly like the one you provided, where the actual design consists of a range of gray colors, and the text is always black. If I were to go about this I would do the following process: The solutions here may not work for your case, but I recommend trying them out. I did it myself when I tried their code for my own separate problem). Although it is not in python, the code can be easily translated from c++ to python (Just look at the API and convert the methods from c++ to python, not hard. I recommend looking at this question here, for it may answer your case as well. There are multiple ways to go about detecting text in an image.
0 Comments
Leave a Reply. |