Image Segmentation to Map Cracks

2024/11/16 2:46:11

I have this problem that I have been working on recently and I have kind of reached a dead end as I am not sure why the image saved when re opened it is loaded as black image. I have (original) as my base image and I am trying to write a code to threshold the crack and make them appear in white while the rest of the image is black. I have managed to go as far as to create such image.

However, I cannot process it further because it consist of 2 channels and all cv2 and PIL libraries works with n = 1 and n = 3 channels images. I figured I can save the image and then loaded again into the system. However, when doing so the image is being loaded as black circle even though it appears that it was saved correctly in my system. The saved image (Saved) shows my image when opening it with windows photo, which doesn't look like what it seems when it is saved. However, when opening it with windows paint it looks correct and when I save it and loaded back into my python code everything works fine.

So my question is there a way to open each saved image in paint save it and close it so I can further process them, or is there another way around better than my method. The resulted image I am looking to have is shown in FinalImage.

I would appreciate if one can help me with that. For some reason I cant seem to upload the images and the code at the same time. I don't know why

import cv2
import numpy as np
from PIL import Image, ImageDraw
from PIL import Image, ImageOps#Read Image
img = cv2.imread('Input-Set/test_test_recon_Export0171.tif')# Convert into gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# Image processing ( smoothing )
# Averaging
blur = cv2.blur(gray,(3,3))#Threshold The Image
ret,th1 = cv2.threshold(blur,145,255,cv2.THRESH_BINARY)
inverted = np.invert(th1)#Threshold The Background
ret,th2 = cv2.threshold(blur,1,255,cv2.THRESH_BINARY)
#inverted2 = np.invert(th2)#Mask Background over Threshold Image
masked = cv2.bitwise_and(inverted, inverted, mask=th2)#Crop AOI
height = 998
width = 1024
lum_img = Image.new('L', [height,width] , 0)draw = ImageDraw.Draw(lum_img)
draw.pieslice([(16,37), (960,985)], 0, 360, fill = 255, outline = "white")
img_arr =np.array(th1)
lum_img_arr =np.array(lum_img)final_img_arr = np.dstack((lum_img_arr,img_arr))
FinalImage = Image.fromarray(final_img_arr)
inverted2 = np.invert(FinalImage)
FinalImage2 = Image.fromarray(inverted2)Image.fromarray(inverted2).save('Output-Set/result.tif')
# img2 = Image.open("Output-Set/result.tif")
img2 = cv2.imread('Output-Set/result.tif')

Original

Original

Saved

Saved

Final Image

FinalImage

Answer

Here is one simple way in Python/OpenCV. Just use cv2.inRange() to do the threshold on the crack's gray value.

Input:

enter image description here

import cv2# load image
img = cv2.imread("cracks.png")# threshold using on dark gray
lower = (1,1,1)
upper = (120,120,120)
thresh = cv2.inRange(img, lower, upper)# write result to disk
cv2.imwrite("cracks_threshold.png", thresh)# display it
cv2.imshow("thresh", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

Threshold result:

enter image description here

https://en.xdnf.cn/q/119580.html

Related Q&A

operations on column length

Firstly, sorry if I have used the wrong language to explain what Im operating on, Im pretty new to python and still far from being knowledgeable about it.Im currently trying to do operations on the len…

Python: Parse one string into multiple variables?

I am pretty sure that there is a function for this, but I been searching for a while, so decided to simply ask SO instead.I am writing a Python script that parses and analyzes text messages from an inp…

How do I pull multiple values from html page using python?

Im performing some data analysis for my own knowledge from nhl spread/betting odds information. Im able to pull some information, but Not the entire data set. I want to pull the list of games and the a…

Creating h5 file for storing a dataset to train super resolution GAN

I am trying to create a h5 file for storing a dataset for training a super resolution GAN. Where each training pair would be a Low resolution and a High resolution image. The dataset will contain the d…

How to resolve wide_to_long error in pandas

I have following dataframeAnd I want to convert it into the following format:-To do so I have used the following code snippet:-df = pd.wide_to_long(df, stubnames=[manufacturing_unit_,outlet_,inventory,…

Odoo 10: enter value in Many2one field dynamically

I added in my models.py :commercial_group = fields.Many2one("simcard.simcard")and in my views.xml :<field name="commercial_group" widget="selection"/>And then i am t…

How to erode this thresholded image using OpenCV

I am trying to first remove the captcha numbers by thresholding and then eroding it ,to get slim continuous lines to get better output. Problem:the eroded image is not continuous as u can see Original …

Searching for only the first value in an array in a csv file

So i am creating a account login system which searches a database for a username (and its relevant password) and, if found, will log the user on.This is what the csv file currently looks like[dom, ente…

how to write a single row cell by cell and fill it in csv file

I have a CSV file that only has column headers:cat mycsv.csvcol_1@@@col_2@@@col_3@@@col_3I have to fill a single row with None values in each cell of the CSV file. Can someone suggest me the best-optim…

Greedy String Tiling in Python

I am trying to learn greedy string tiling in algorithmI have two lists as follows:a=[a,b,c,d,e,f] b=[d,e,a,b,c,f]i would like to retrieve c=[a,b,c,d,e]Another example would be a = [1,2,3,4,5,6,7,8,9,1,…