

But for a startup, hope it is OK.Īs many have alluded in the comments, the best way is to invert the image so the black text becomes white, find all the non-zero points in the image then determine what the minimum spanning bounding box would be.

#Draw matched key points on template imageīelow are the results I got (copy pasted template image on original image using paint):Īs you can see, there are some small mistakes. #Draw matched key points on original image

If dist<0.1: # draw matched keypoints in red color

Retval, results, neigh_resp, dists = knn.find_nearest(des,1) Keys,desc = tect(templateg,None,useProvidedKeypoints = False)ĭes = np.array(des,np.float32).reshape((1,128)) Templateg= cv2.cvtColor(template,cv2.COLOR_BGR2GRAY) # Now loading a template image and searching for similar keypoints Responses = np.arange(len(kp),dtype = np.float32) # Setting up samples and responses for kNN Kp, descritors = tect(imgg,None,useProvidedKeypoints = False) Imgg =cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) But if you are looking for a sample of matching SURF keypoints, a very simple and basic one is below, which is similar to template matching: import cv2 I am not sure whether i understand your questions correctly. Other values for BORDER_TYPE are possible, such as BORDER_DEFAULT, BORDER_REPLICATE, BORDER_WRAP. If you leave bordersize values for bottom and right at 0, you even get a symmetric border. Sorry, somewhat hard coded, but shows the general how-to and can be adapted to your needs.
#ADD BORDER TO TEXT OPENCV CODE#
Other libraries may outperform the OpenCVs rendering system.The following code adds a constant border of size 10 pixels to all four sides of your original image.įor the colour, I have assumed that you want to use the average gray value of the background, which I have calculated from the mean value of bottom two lines of your image. PutText(image, "OpenCV", roi, color, fontFace, fontScale, thickness) Īs a result of the PutText() function you can render any text over an arbitrary ROI of the image, such as:Īnd keep in mind that text rendering (with or without this trick) in OpenCV is very expensive and can affect to the runtime of your applications. Int fontFace = cv::FONT_HERSHEY_SCRIPT_SIMPLEX If (color = cv::Scalar::all(0)) cv::threshold(textImgMask, textImgMask, 1, 255, cv::THRESH_BINARY_INV) Įlse cv::threshold(textImgMask, textImgMask, 254, 255, cv::THRESH_BINARY) Īnd call it like: cv::Mat image = cv::imread("C:/opencv_logo.png") Ĭv::Rect roi(5, 5, ls - 5, image.rows - 5) Estimating the resolution of bounding imageĬv::Point textOrg((ls - textSize.width) / 2, (textImg.rows + textSize.height - baseline) / 2) Ĭv::Point tr(textOrg.x, textOrg.y + baseline) Ĭv::Point bl(textOrg.x + textSize.width, textOrg.y - textSize.height) Ĭv::putText(textImg, text, textOrg, fontFace, fontScale, color, thickness) Ĭv::resize(textImg, textImg, roi.size()) Ĭv::cvtColor(textImgMask, textImgMask, cv::COLOR_BGR2GRAY) Ĭv::equalizeHist(textImgMask, textImgMask) Render the text over here (fits to the text size)Ĭv::Mat textImg(textSize.height + baseline, textSize.width, img.type()) Y-coordinate of the baseline relative to the bottom-most text point Calculates the width and height of a text stringĬv::Size textSize = cv::getTextSize(text, fontFace, fontScale, thickness, &baseline) If you'd like to render the text in an arbitrary ROI of an image then you first need to render it into another image (which fits to the text size), resize it to the ROI desired and then put it over the image, such as below: void PutText(cv::Mat& img, const std::string& text, const cv::Rect& roi, const cv::Scalar& color, int fontFace, double fontScale, int thickness = 1, int lineType = 8)ĬV_Assert(!img.empty() & (img.type() = CV_8UC3 || img.type() = CV_8UC1)) There is a sample code in the description of the function but it only renders some text, the tight box (surrounding the text), and the baseline of it. It's a bit tricky, but you can play with cv::getTextSize().
