I want to extract stripes from this sample file sample file, and the result should look like this one similar result image. Then, I need to count the number of stripes on the right, and calculate the distance from the end of each left stripe to the end of each adjacent right stripe.
I tried with the following code, but my result my result fileis still a little bit away from my target. Here is what I do:
import numpy as np
import cv2
from matplotlib import pyplot as plt
gray = cv2.imread('input_file.png',cv2.IMREAD_UNCHANGED)
sobelY = cv2.Sobel(gray, cv2.CV_32F, 0, 1, ksize=3)
sobelY2 = cv2.Sobel(sobelY, cv2.CV_32F, 0, 1, ksize=3)
sobelY2[sobelY2<0]=0
mask = np.where(sobelY2==0,0,1)
sobelY2 = cv2.normalize(sobelY2, dst=None, alpha=0, beta=65535, norm_type=cv2.NORM_MINMAX).astype(np.uint16)
clahe=cv2.createCLAHE(clipLimit=6, tileGridSize=(8,8))
sobelY2_clahe = clahe.apply(sobelY2)
sobelY2_clahe = clahe.apply(sobelY2_clahe)
result = np.where(mask!=0,sobelY2_clahe,0)
fig = plt.figure(figsize=(10, 10))
ax = plt.subplot(121)
plt.imshow(gray, cmap='gray')
ax = plt.subplot(122)
plt.imshow(result, cmap='gray')
plt.show()
The input file is in 16 bits format, so I keep it unchanged for accuracy. I do second order Sobel operation in Y direction to high light those stripes, and then I do two times Clahe operations to balance the contrast. To keep the background pixels as 0, I use a mask to set the values back after the Clahe operations.
Any advice is appreciated!
For completeness, I am attaching another more challenged input file for referencemore challenged input file.
Edit:
The sobelY2 image pretty much reflects the stripes, but could we make it look better?
I just opened a new question about how to trim each of these stripes based on gray scale values.trim image based on grayscale values
Related
There is a electron microscope photo of some surface:
What I want to do is to use OpenCV to detect edges of pyramid in this image and measure their length. Currently I have to label them manually (the red lines in image).
I try to use Canny with Hough transform but the result turn out not very well.
import cv2
from matplotlib import pyplot as plt
import numpy as np
img = cv2.imread('../data/sample.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
kernel_size = 9
blur_gray = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)
lo, hi = 15, 27
edges = cv2.Canny(blur_gray, lo, hi)
plt.figure()
plt.imshow(blur_gray)
plt.figure()
plt.imshow(edges)
lines
= cv2.HoughLinesP(edges, 1, np.pi / 180, 50, None, 50, 10)
It looks to me like there is too much noise in the original image. What I want to detect is the lateral edges of pyramid, but the base edges are also included with my code. I don't know how to remove them before using Canny and Hough transform. And there are some glitches in the result of edge detection which I don't know how to eliminate them. I though Gaussian blur should be enough but it turns out not working very well.
I have to confess that I have little knowledge of computer vision so I am not sure what's the right tool for this. It would be really appreciated to have someone shed some light on it. There is no need to find a 100% accuracy method as there are some abnormal structures in the image that have to be adjust manually, what I want is try to reduce the human effort as much as possible by using some computer vision technique.
I want to draw find the depth of the drawed region and draw the boundaries I tried to use Canny edge detection, but it is not good to get the edges. Are there ways to get the pixel of the boundary of interest? The pixel intensity (in grayscale) in the boundary line is not nuch different from the surrounding area.
The region of interest is melting pool on a metal. The purpose is to find the depth of the melting pool. I tried Canny edge detection but it seems not work to solve the problem.
Are there other ways using python to coordinates of boundary of melting pool boundary which I colored in red in picture 2?Original image Region of interest (in red)
Canny edge detection The melting pool is moving. I want to use python to get the depth change of the melting pool. I have bunch of images
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('image.tif',0)
edges = cv2.Canny(img,100,20)
plt.subplots(),plt.imshow(img,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplots(),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()
I think the best way to obtain the desired information about the metal pool is to segment it. Since the image is noisy, I think graph-cut is the better option.
I estimate the pool boundary with the vertical Scharr filtering and use them to compute the graph arc-weight.
From this, I use the upper and lower bourders of the image as source and sink for the graph-cut algorithm (these pixels will belong to different labels).
Second segmentation is performed to obtain the horizontal line without the pool and compute their difference to obtain the final result.
The beta parameter must be tuned, as it increases it will adhere more to your weights (noisy boundary). I found that 50 gets good results, but you should play with it.
import numpy as np
from skimage import io, filters, measure
import skimage.morphology as morph
import matplotlib.pyplot as plt
import maxflow
def normalize(im):
im -= im.min()
return im / im.max()
def graph_cut(weights):
g = maxflow.GraphFloat()
nodeids = g.add_grid_nodes(weights.shape)
structure = maxflow.vonNeumann_structure(ndim=2, directed=True)
g.add_grid_edges(nodeids, weights, structure=structure, symmetric=True)
g.add_grid_tedges(nodeids[1, :], 0, 1e16)
g.add_grid_tedges(nodeids[-1, :], 1e16, 0)
g.maxflow()
return g.get_grid_segments(nodeids)
def get_largest(label):
label = measure.label(label)
largest = label == np.argmax(np.bincount(label.flat)[1:])+1
return largest
def main():
im = io.imread("example.png")
im = filters.median(im, morph.disk(5))
# pool segmentation
beta = 50 # parameter
aux = filters.scharr_v(im)
aux = normalize(np.abs(aux))
weights = np.exp(-beta * aux)
pool = graph_cut(weights)
# end
# surface segmentation
aux = np.abs(filters.scharr(im))
aux = normalize(aux)
weights = np.exp(-aux)
surf = graph_cut(weights)
# end
# result
res = pool ^ surf # xor
res = get_largest(res)
contours = measure.find_contours(res, 0.5)
fig, ax = plt.subplots()
ax.imshow(im, cmap='gray')
for contour in contours:
ax.plot(contour[:, 1], contour[:, 0], linewidth=1, c = 'red')
plt.show()
if __name__ == "__main__":
main()
Results:
Strong horizontal lowpass filtering will improve the signal-to-noise ratio and make the top edge easy to detect.
Note that straight binarization of the raw image performs even better.
Adaptive thresholding is interesting as well, though requires some tuning.
I've been recently working at a segmentation process for corneal
endothelial cells, and I've found a pretty decent paper that describes ways to perform it with nice results. I have been trying to follow that paper and implement it all using scikit-image and openCV, but I've gotten stucked at the watershed segmentation.
I will briefly describe how is the process supposed to be:
First of all, you have the original endothelial cells image
original image
Then, they instruct you to perform a morphological grayscale reconstruction, in order to level a little bit the grayscale of the image (however, they do not explain how to get the markers for the grayscale, so I've been fooling around and tried to get some on my own way)
This is what the reconstructed image was supposed to look like:
desired reconstruction
This is what my reconstructed image (lets label it as r) looks like:
my reconstruction
The purpose is to use the reconstructed image to get the markers for the watershed segmentation, how do we do that?! We get the original image (lets label it as f), and perform a threshold in (f - r) to extract the h-domes of the cell, i.e., our markers.
This is what the hdomes image was supposed to look like:
desired hdomes
This is what my hdomes image looks like:
my hdomes
I believe that the hdomes I've got are as good as theirs, so, the final step is to finally perform the watershed segmentation on the original image, using the hdomes we've been working so hard to get!
As input image, we will use the inverted original image, and as markers, our markers.
This is the derised output:
desired output
However, I am only getting a black image, EVERY PIXEL IS BLACK and I have no idea of what's happening... I've also tried using their markers and inverted image, however, also getting black image. The paper I've been using is Luc M. Vincent, Barry R. Masters, "Morphological image processing and network analysis of cornea endothelial cell images", Proc. SPIE 1769
I apologize for the long text, however I really wanted to explain everything in detail of what is my understanding so far, btw, I've tried watershed segmentation from both scikit-image and opencv, both gave me the black image.
Here is the following code that I have been using
img = cv2.imread('input.png',0)
mask = img
marker = cv2.erode(mask, cv2.getStructuringElement(cv2.MORPH_ERODE,(3,3)), iterations = 3)
reconstructedImage = reconstruction(marker, mask)
hdomes = img - reconstructedImage
cell_markers = cv2.threshold(hdomes, 0, 255, cv2.THRESH_BINARY)[1]
inverted = (255 - img)
labels = watershed(inverted, cell_markers)
cv2.imwrite('test.png', labels)
plt.figure()
plt.imshow(labels)
plt.show()
Thank you!
Here's a rough example for the watershed segmentation of your image with scikit-image.
What is missing in your script is calculating the Euclidean distance (see here and here) and extracting the local maxima from it.
Note that the watershed algorithm outputs a piece-wise constant image where pixels in the same regions are assigned the same value. What is shown in your 'desired output' panel (e) are the edges between the regions instead.
import numpy as np
import cv2
import matplotlib.pyplot as plt
from skimage.morphology import watershed
from scipy import ndimage as ndi
from skimage.feature import peak_local_max
from skimage.filters import threshold_local
img = cv2.imread('input.jpg',0)
'''Adaptive thersholding
calculates thresholds in regions of size block_size surrounding each pixel
to handle the non-uniform background'''
block_size = 41
adaptive_thresh = threshold_local(img, block_size)#, offset=10)
binary_adaptive = img > adaptive_thresh
# Calculate Euclidean distance
distance = ndi.distance_transform_edt(binary_adaptive)
# Find local maxima of the distance map
local_maxi = peak_local_max(distance, labels=binary_adaptive, footprint=np.ones((3, 3)), indices=False)
# Label the maxima
markers = ndi.label(local_maxi)[0]
''' Watershed algorithm
The option watershed_line=True leave a one-pixel wide line
with label 0 separating the regions obtained by the watershed algorithm '''
labels = watershed(-distance, markers, watershed_line=True)
# Plot the result
plt.imshow(img, cmap='gray')
plt.imshow(labels==0,alpha=.3, cmap='Reds')
plt.show()
I want to find the orientation of the bright object in the images attached. For this purpose, I used Principal Component Analysis(PCA).
In case of image 1, PCA finds correct orientation as the first principal component is alligned in that direction. However, in case of image 2, the principal components are disoriented.
Can anyone please explain why the PCA is showing different results in the two images? Also, please suggest if there is some other method to find the orientation of the object.
import os
import gdal
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import skimage
from skimage.filters import threshold_otsu
from skimage.filters import try_all_threshold
import cv2
import math
from skimage import img_as_ubyte
from skimage.morphology import convex_hull_image
import pandas as pd
file="path to image file"
(fileRoot, fileExt)= os.path.splitext(file)
ds = gdal.Open(file)
band = ds.GetRasterBand(1)
arr = band.ReadAsArray()
geotransform = ds.GetGeoTransform()
[cols, rows] = arr.shape
thresh = threshold_otsu(arr)
binary = arr > thresh
points = binary>0
y,x = np.nonzero(points)
x = x - np.mean(x)
y = y - np.mean(y)
coords = np.vstack([x, y])
cov = np.cov(coords)
evals, evecs = np.linalg.eig(cov)
sort_indices = np.argsort(evals)[::-1]
evec1, evec2 = evecs[:, sort_indices]
x_v1, y_v1 = evec1
x_v2, y_v2 = evec2
scale = 40
plt.plot([x_v1*-scale*2, x_v1*scale*2],
[y_v1*-scale*2, y_v1*scale*2], color='red')
plt.plot([x_v2*-scale, x_v2*scale],
[y_v2*-scale, y_v2*scale], color='blue')
plt.plot(x,y, 'k.')
plt.axis('equal')
plt.gca().invert_yaxis()
plt.show()
theta = np.tanh((x_v1)/(y_v1)) * 180 /(math.pi)
You claim you are using just white pixels. Did you check which ones are selected by some overlay render? Anyway I do not think it is enough especially for your second image as it does not contain any fully saturated white pixels. I would use more processing before the PCA.
enhance dynamic range
your current images does not need this step as they contain both black and almost fully saturated white. This step allow to unify threshold values among more sample input images. For more info see:
Enhancing dynamic range and normalizing illumination
smooth a bit
this step will significantly lover the intensity of noise points and smooth the edges of bigger objects (but shrink them a bit). This can be done by any FIR filter or convolution or Gaussian filtering. Some also use morphology operators for this.
threshold by intensity
this will remove darker pixels (clear to black) so noise is fully removed
enlarge remaining objects by morphology operators back to former size
You can avoid this by enlarging the resulting OBB by few pixels (number is bound to smooth strength from #2).
now apply OBB search
You are using PCA so use it. I am using this instead:
How to Compute OBB of Multiple Curves?
When I tried your images with above approach (without the #4) I got these results:
Another problem I noticed with your second image is that there are not many white pixels in it. That may bias the PCA significantly especially without preprocessing. I would try to enlarge the image by bicubic filtering and use that as input. May be that is the only problem you got with it.
I have an image that is a simple picture and I want to extract the end points of the lines. However, there are other lines that do overlap, so, my lines have 'gaps' in them.
I am trying to use HoughLinesP to extract the parameterization of these ten lines and though the visual result is reasonable, it still gives me 43 individual lines.
I have tried smoothing the lines, skeleton representation of the lines, redrawing the lines after each of those, I'm working with countours right now. I have adjusted my parameters (line length, max gap, threshold, etc...) and I can not get this to reduce to ten lines. In my current code, I subtract the first image from itself to make a new black space to draw my HoughLines on, maybe not the most efficient, but its effective. Here is my code:
import numpy as np
import cv2
img = cv2.imread('masked.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 100
img2 = cv2.subtract(img, img)
lines = cv2.HoughLinesP(gray,1,np.pi/180,10,minLineLength,maxLineGap=100)
print(len(lines))
for n in range(len(lines)):
for x1,y1,x2,y2 in lines[n]:
cv2.line(img2,(x1,y1),(x2,y2),(0,255,0),1,4)
cv2.imshow('result.png', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
Is there another approach that would allow me to fill in these gaps and pull out ten equations of lines? I'm using Python, OpenCV and Numpy right now.