hoogldel.blogg.se

Add border to text opencv
Add border to text opencv







add border to text opencv

There is a trade-off when increasing or decreasing the kernel size as you may capture more or less of the lines. Similarly, you could modify the vertical kernels to detect more or less vertical lines. In addition, you could increase the number of iterations when performing cv2.morphologyEx(). For the colour, I have assumed that you want to use the.

#ADD BORDER TO TEXT OPENCV CODE#

If you wanted to detect thicker horizontal lines, then you could increase the width of the kernel to say (80, 2). The following code adds a constant border of size 10 pixels to all four sides of your original image. For instance to capture longer horizontal lines, it may be necessary to increase the horizontal kernel from (40, 1) to say (80, 1). Note: Depending on the image, you may have to modify the kernel size. Remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)Ĭnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)Īfter filling in both horizontal and vertical lines with white, here's our result Vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,40))

add border to text opencv

Here's the detected vertical lines highlighted in green # Remove vertical lines Similarly we create a vertical kernel to remove the vertical lines, find contours, and fill each vertical contour with white. Remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)Ĭnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)Ĭnts = cnts if len(cnts) = 2 else cntsĬv2.drawContours(result,, -1, (255,255,255), 5) Horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1)) Here's the detected horizontal lines in green # Remove horizontal lines This effectively "erases" the horizontal line. To remove the horizontal lines, we use cv2.drawContours()Īnd fill in each horizontal contour with white. Now we create a horizontal kernel to detect horizontal lines with cv2.getStructuringElement() and find contours with cv2.findContours() Thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) Gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) Load image, convert to grayscale, then Otsu's threshold to obtain a binary image image = cv2.imread('1.png') We do the same operation but with a vertical shaped kernel printf ( ' Press 'r' to set the border to be replicated ' ) printf ( ' Press 'ESC' to exit the program ' ) namedWindow ( windowname, WINDOWAUTOSIZE ) // Initialize arguments for the filter. We create a horizontal shaped kernelĪnd remove the lines with cv2.drawContours() Load the image, convert to grayscale, and Otsu's threshold

add border to text opencv

Be careful when saving as an image file with Pillow because the data type is cast automatically.Since no one has posted a complete OpenCV solution, here's a simple approach Since the operation of ndarray and scalar value is the operation of the value of each element and the scalar value, alpha blend can be calculated as follows. Convert BGR and RGB with Python, OpenCV (cvtColor).Image files are read as ndarray with OpenCV's cv2.imread(), so it doesn't matter which OpenCV or Pillow is used, but be aware that the color order is different. Resize is also done by the method of Pillow. Here, image files are read as NumPy array ndarray using Pillow. Since NumPy can easily perform arithmetic operations for each pixel of the array, alpha blending can also be realized with a simple expression. In addition to cv2.bitwise_and(), OpenCV also includes cv2.bitwise_or(), cv2.bitwise_xor() and cv2.bitwise_not() for performing OR, XOR and NOT operation. It may be easier to understand the mask processing with NumPy described later. In the case of uint8, the result of the bit operation is easy to understand, but in the case of the floating point number float, it is noted that the bit operation is performed in binary notation, and the result is unexpected. When the image file is read, the data type is uint8 (unsigned 8-bit integer: 0-255), black indicates pixel value 0 ( 0b00000000 in binary), white indicates pixel value 255 ( 0b11111111 in binary) ).









Add border to text opencv