1

I have a grayscale image and want to remove the white rectangle from the image and join the upper and lower images. The input image isenter image description here The output image should look likeenter image description here

Code that I have done so far

image = cv2.imread('./rotated_image.png')

mask = np.zeros(image.shape[:2], np.uint8)

backgroundModel = np.zeros((1, 65), np.float64)
foregroundModel = np.zeros((1, 65), np.float64)
 
rectangle = (78, 10, 971, 30)
 
cv2.grabCut(image, mask, rectangle, backgroundModel, foregroundModel,5, cv2.GC_INIT_WITH_RECT)
 
mask2 = np.where((mask == 2)|(mask == 0), 0, 1).astype('uint8')

image = image * mask2[:, :, np.newaxis]
plt.imshow(image)
plt.show()
cv2.imwrite("op.png", image)

so the output is:

img

After that, I tried subtracting from the main image

image1 = cv2.imread('./rotated_image.png')
image2 = cv2.imread('./op.png')
subtracted = cv2.subtract(image1, image2)
plt.imshow(subtracted)
plt.show()
cv2.imwrite("sub.png", subtracted)

The output is enter image description here

So, the white rectangle becomes black but I want to remove it and merge the image.

Bilal
  • 3,191
  • 4
  • 21
  • 49
Krupali Mistry
  • 624
  • 1
  • 9
  • 23
  • Ok, so where are you having trouble? What have you tried? You are supposed to do your own research and develop your own approach. Stack Overflow is not a free coding service. – stateMachine Feb 07 '22 at 04:55
  • I have tried thresholding and making the white part black but the problem is I am unable to remove that particular part. – Krupali Mistry Feb 07 '22 at 05:02
  • 1
    Thresholding is a good start. Maybe use Otsu's Thresholding to segment the brightest part of the image. Get its contour and then its bounding box. Now, with this info you estimate two bounding boxes more: one for the top part and another for the bottom part. Crop the two parts and merge them in a new image. – stateMachine Feb 07 '22 at 05:05
  • I have tried using the grabcut method.But I am confused here. – Krupali Mistry Feb 07 '22 at 05:07
  • @KrupaliMistry you can **sum** the row values, and for a specified **threshold** you can delete that row to remove the white and keep the gray region. – Bilal Feb 07 '22 at 07:14
  • you talk of removing entire rows of pixels. that means you need to slice and concatenate/vconcat/vstack. – Christoph Rackwitz Feb 07 '22 at 12:26
  • @ChristophRackwitz I want to remove the white part from the image and merge the remaining image – Krupali Mistry Feb 08 '22 at 06:12
  • @Bilal can you give an example of summing the values and thresholding it – Krupali Mistry Feb 08 '22 at 06:14
  • @KrupaliMistry here is an [example with barcode](https://stackoverflow.com/a/64175996), the same approach is applicable here. – Bilal Feb 08 '22 at 06:51

0 Answers0