U-Net predicted masks are all black - machine-learning

I've trained a U-Net to segment some leaves images with a symptom of a agricultural pest.. so I got 3 classes, backgroud (0), leaf(1) and the symptom (3).
When I predict all test images, I got a black image in 8bit.
So when I opened (IMAGEJ) them and changed the type to 16-bit I can see the image itself and all 3 classes predicted, not a black square anymore...
How can I predict the masks correctly?
All test and train images are in RGB, and all maks are in 8bit (but in here, the classes are shown)
import numpy as np
import cv2
import os
import glob
#Process each patch in test database
img_number = 1
for image in range(test_images.shape[0]):
input_img = [test_images]
y_pred = model.predict(input_img)
y_pred_argmax=np.argmax(y_pred, axis=3)
prediction = y_pred_argmax[image]
cv2.imwrite('/content/drive/MyDrive/BD_filtred/predicted'+str(img_number)+".png", prediction)
img_number +=1

Related

Separating overlaying colors in opencv

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.

Iterate all over the pixels of the binary image in c# with OpencvSharp

How can I iterate all over pixels of this binary image.
I want to skeletonize or thinning this "white" line but still do not know how to iterate all over the pixels
I'm not actually know what kind of "skeletonize", "thinning" you mean.
This is a simple way to make it s
import cv2
import numpy as np
img = cv2.imread("B0xTI.png")
kernel = np.ones((3,3), np.uint8)
thinning = cv2.erode(img, kernel, iterations = 2)
cv2.imwrite("thinning.jpg",thinning)
output image:

Critical parameter behind skimage's watershed "over-segmentation"

I have the following mask of cell nuclei, and my goal is to segment them. However, using what seems to be a very standard approach,
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from skimage.segmentation import watershed
from skimage import measure
# load mask
mask = mpimg.imread('mask.png')
# find distance to nearest border
distance = scipy.ndimage.distance_transform_edt(mask)
# find local maxima based on distance to border
local_maxi = peak_local_max(distance, indices=False, footprint=np.ones((125, 125)), labels=mask)
# generate markers for regions
markers = measure.label(local_maxi)
# watershed segmentation
labeled = watershed(-distance, markers, mask=mask, watershed_line = True)
# plot figure
fig, axs = plt.subplots()
axs.imshow(labeled, cmap='flag')
some large, connected components are unsegmented while smaller unconnected components become oversegmented:
Throughly browosing answers on StackOverflow, I haven't been able to find is a discussion of which parameters drive 'under-segmentation' vs 'over-segmentation' in the skimage.segmentation.watershed algorithm.
Which parameter most strongly influences "oversegmentation" in the watershed algorithm? My intuition tells me it could be the footprint size? or the distance transform? What is the most critical parameter that determines the segmentation neighbourhood?
EDIT1: Below I have included the distance transform, the filtering of which others have pointed out is a critically important step. However, I am still unable to diagnose symptoms of a "bad" distance transform, and unaware of rules of thumbs for filtering said transform.
In your particular case, the origin of some of your over-segmentation is on the result of peak_local_max().
If you run the following code you will be able to find which local maximums are selected for your image. I'm using OpenCV for plotting dots, you might want to adapt it for another library.
import cv2
import numpy as np
import matplotlib.pyplot as plt
localMax_idx = np.where(local_maxi)
localMax_img = mask.copy()
localMax_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
for i in range(localMax_idx[0].shape[0]):
x = localMax_idx[1][i]
y = localMax_idx[0][i]
localMax_img = cv2.circle(localMax_img, (x,y), radius=5, color=(255, 0, 0), thickness=-1)
plt.imshow(localMax_img)
plt.show()
You will see that there are multiple markers for over-segmented cells. There are some suggested approaches to deal with this issue (for example, this one).

watershed segmentation always return black image

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()

Find orientation of object using PCA

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.

Resources