I have to create a linear grayscale gradient, with black shade on top and white shade at the bottom. I have to use skimage and numpy.
I've found on scikit the code for a color linear gradient that goes horizontally instead of vertically here: http://scikit-image.org/docs/dev/auto_examples/plot_tinting_grayscale_images.html.
I would like an explanation of this code and some hints on how put everything in grayscale and vertical instead of colored and horizontal. Thanks!
A grey-scale image can be represented as a two-dimensional matrix. Let's say we wanted to create a 100x100 gradient image. First, we use np.linspace to construct the gradient values, 100 values between 0 and 1. We then repeat this vector 100 times in the vertical direction (using np.tile) to form the gradient image. At this stage, the image is a gradient from left to right, so we use the transpose to flip it to be oriented up-down.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 1, 100)
image = np.tile(x, (100, 1)).T
plt.imshow(image, cmap='gray')
plt.show()
Related
Let's say I have a picture with two colors, but both their colors are overlapping.
Object A is a star, and object B is a rectangle with a star-like hole. They overlap each other.
Is there a way of separating the objects? Kinda like finding the intersection and summing up to their "pure" standards?
I see two ways of doing this: via shape recognition or via color. Don't know which way would be smarter.
First I tried to separate the colors via histogram in grayscale, such as in BATspock's question
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread("origin.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray],[0],None,[256],[0,256])
colors = np.where(hist>25000)
img_number = 0
for color in colors[0]:
print(color)
split_image = img.copy()
split_image[np.where(gray != color)] = 0
cv2.imwrite(str(img_number)+".jpg",split_image)
img_number+=1
plt.hist(gray.ravel(),256,[0,256])
plt.savefig('plt')
plt.show()
But no success in getting the color of that intersection due to very low Histogram values.
I tried using the example present here, although it renders the same effect I desire, the example just refers to color splitting in RGB. Is there anything similar to this output but choosing the colors instead? Maybe feature recognition? Watershed? I'm lost.
I want to perform an Dilation operation while having rounded corners.
Something like this :
What I tried :
import numpy as np
import cv2
img = cv2.imread(test.jpg)
kernel = np.array([[0,1,0],
[1,1,1],
[0,1,0]], dtype=np.uint8)
img_d = cv2.dilate(img, kernel, iterations=45)
Image used : test.jpg
I tried multiple kernels (with different sizes 3x3, 5x5...) but I didn't succeed to get rounded corners.
My Question is : Can we get rounded corners just by changing the kernel or should we add a further processing step to achieve this ?
NOTE : My goal is not to create a rounded square... I used this example just to explain the idea of getting rounded corners with a
dilation operation.
I figured out a way thanks to "Christoph Rackwitz" comment.
The idea is pretty simple.
We need to use a bigger kernel with a circle shape and reduce the number of Dilation iterations.
import numpy as np
import cv2
kernel = np.zeros((100,100), np.uint8)
cv2.circle(kernel, (50,50), 50, 255, -1)
plt.imshow(kernel, cmap="gray")
And then use this kernel with just one iteration :
img = cv2.imread(test.jpg)
img_d = cv2.dilate(img, kernel, iterations=1)
plt.imshow(kernel, cmap="gray")
OpenCV also allows you choose a kernel of your shape and size with cv2.getStructuringElement. From this page you can choose either a rectangle, ellipse or cross shaped kernels.
Since you needed rounded corners I chose the ellipse kernel cv2.MORPH_ELLIPSE of size 25 x 25:
img = cv2.imread('test.jpg', 0)
th_img = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(25,25))
img_dil = cv2.dilate(th_img, kernel, iterations=5)
Not exactly the result you were hoping for but here is how it looks:
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
I have an image to which I apply a bilateral filter, followed by adaptive thresholding to get the image below.
original image (this is a screenshot off the depth image of the object)
thresholded image
I would like to fit lines to the vertical parts/lines and find the center poiint, output like image below:
I cant seem to understand the output of the cv2.adaptiveThreshold(). How are the purple pixels (i.e my edges) represented? and how can a line be fitted? MWE:
import cv2
image = cv2.imread("depth_frame0009.jpg")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
bilateral_filter = cv2.bilateralFilter(gray_image, 15, 50, 50)
plt.figure()
plt.imshow(bilateral_filter)
plt.title("bilateral filter")
#plt.imsave("2dimage_gaussianFilter.png",blurred)
plt.imsave("depthmap_image_bilateralFilter.png",bilateral_filter)
th3 = cv2.adaptiveThreshold(bilateral_filter,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
plt.figure()
plt.imshow(th3)
========
edit:
Canny edges
contours
They are represented as an image, a matrix of uint8.
The reason it is purple and yellow is because matplotlib is applying a colormap to it.
I generally prefer to use some specific parameters when plotting image processing output images, eg
plt.imshow(th3, cmap='gray', interpolation='nearest')
If you are specifically interested in finding and fitting lines you may want to use a different representation, such as Hough lines. Once you have the lines in the image you can take the best fit lines and find your center point between them.
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()