1

How I can corp the image or other methods so that the summation of pixels in the boundary, be less than the hole x and y position. I mean in the images below

the white hole summation x and y of the pixels must be higher in the hole position but as shown in the chart the surrounding of the image especially the x summation of pixels have the higher summation

In this way, I want to found the sum pixels that have high values and illustrate the hole pixels or coordination. In the y-axis, the summation of course so as the below better result than the x one but in some others not, of course, I crop all of 1000 image by below code then after returning the pixel data sum of them and the id and the max values than return the hole is illustrated.

img = Image.open('J:\py.pro\path\picture_1.png').convert('L')  # convert image to 8-bit grayscale

if img.mode == "CMYK":
    # color profiles can be found at C:\Program Files (x86)\Common Files\Adobe\Color\Profiles\Recommended
    img = ImageCms.profileToProfile(img, "USWebCoatedSWOP.icc", "sRGB_Color_Space_Profile.icm", outputMode="RGB")
# PIL image -> OpenCV image; see https://stackoverflow.com/q/14134892/2202732
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)

## (1) Convert to gray, and threshold
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)

## (2) Morph-op to remove noise
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10,30))
morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel)

## (3) Find the max-area contour
cnts = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
cnt = sorted(cnts, key=cv2.contourArea)[-1]

## (4) Crop and save it
x,y,w,h = cv2.boundingRect(cnt)
dst = img[y:y+h, x:x+w]

# add border/padding around the cropped image
# dst = cv2.copyMakeBorder(dst, 10, 10, 10, 10, cv2.BORDER_CONSTANT, value=[255,255,255])

#cv2.imshow("J:\\py.pro\\path\\pic_1.png", dst)
cv2.imwrite("J:\\py.pro\\path\\pic_1.png", dst)

cv2.waitKey(0)
cv2.destroyAllWindows()
babak ab
  • 21
  • 4

1 Answers1

2

How about adding a 1-pixel wide white border on all sides and flood-filling white-coloured pixels with black starting from (0,0) so that the flood-fill flows around the edges filling all white areas at image edges?

I'll demonstrate with magenta for the flood-fill so you can see which pixels are affected:

enter image description here


I actually did that with ImageMagick in Terminal, but you can do just the same with OpenCV:

magick lattice.png -bordercolor white -border 1 -fill magenta -draw 'color 0,0 floodfill' result.png
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Sorry, I do not understand your code does it a python code and is it possible to add it to the program? – babak ab Aug 07 '21 at 17:15
  • Does identifying the magenta pixels help you? – Mark Setchell Aug 07 '21 at 17:18
  • Apparently, The chart shows the effect of it on bourders – babak ab Aug 07 '21 at 17:46
  • But how I can apply to all images? does it possible to do it by crop and shade the white of boundary or transparent it? – babak ab Aug 07 '21 at 17:49
  • 1
    Ok, you can use `copyMakeBorder()` to add a 1-pixel white border all the way around https://stackoverflow.com/a/36259457/2836621 or create a white image 2 pixels taller and 2 pixels wider than your initial image then paste in your original image starting at (1,1). Then use `floodFill()` – Mark Setchell Aug 07 '21 at 17:50
  • Thanks, let me check your guidance and help, and if I get confused, I will let you know again – babak ab Aug 07 '21 at 18:35
  • Thank you very much Mark, you are a great helper and also excellent in image processing with your help I can handle the images – babak ab Aug 08 '21 at 07:51