Use the OpenCV function cv::filter2D in order to perform some laplacian filtering for image sharpening
Use the OpenCV function cv::distanceTransform in order to obtain the derived representation of a binary image, where the value of each pixel is replaced by its distance to the nearest background pixel
Use the OpenCV function cv::watershed in order to isolate objects in the image from the background
Theory
Code
C++
This tutorial code's is shown lines below. You can also download it from here.
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
usingnamespacestd;
usingnamespacecv;
intmain(intargc, char *argv[])
{
// Load the imageCommandLineParserparser( argc, argv, "{@input | cards.png | input image}" );
Matsrc = imread( samples::findFile( parser.get<String>( "@input" ) ) );
if( src.empty() )
{
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
// Show the source imageimshow("Source Image", src);
// Change the background from white to black, since that will help later to extract// better results during the use of Distance TransformMatmask;
inRange(src, Scalar(255, 255, 255), Scalar(255, 255, 255), mask);
src.setTo(Scalar(0, 0, 0), mask);
// Show output imageimshow("Black Background Image", src);
// Create a kernel that we will use to sharpen our imageMatkernel = (Mat_<float>(3,3) <<
1, 1, 1,
1, -8, 1,
1, 1, 1); // an approximation of second derivative, a quite strong kernel// do the laplacian filtering as it is// well, we need to convert everything in something more deeper then CV_8U// because the kernel has some negative values,// and we can expect in general to have a Laplacian image with negative values// BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255// so the possible negative number will be truncatedMatimgLaplacian;
filter2D(src, imgLaplacian, CV_32F, kernel);
Matsharp;
src.convertTo(sharp, CV_32F);
MatimgResult = sharp - imgLaplacian;
// convert back to 8bits gray scaleimgResult.convertTo(imgResult, CV_8UC3);
imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
// imshow( "Laplace Filtered Image", imgLaplacian );imshow( "New Sharped Image", imgResult );
// Create binary image from source imageMatbw;
cvtColor(imgResult, bw, COLOR_BGR2GRAY);
threshold(bw, bw, 40, 255, THRESH_BINARY | THRESH_OTSU);
imshow("Binary Image", bw);
// Perform the distance transform algorithmMatdist;
distanceTransform(bw, dist, DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}// so we can visualize and threshold itnormalize(dist, dist, 0, 1.0, NORM_MINMAX);
imshow("Distance Transform Image", dist);
// Threshold to obtain the peaks// This will be the markers for the foreground objectsthreshold(dist, dist, 0.4, 1.0, THRESH_BINARY);
// Dilate a bit the dist imageMatkernel1 = Mat::ones(3, 3, CV_8U);
dilate(dist, dist, kernel1);
imshow("Peaks", dist);
// Create the CV_8U version of the distance image// It is needed for findContours()Matdist_8u;
dist.convertTo(dist_8u, CV_8U);
// Find total markersvector<vector<Point> > contours;
findContours(dist_8u, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// Create the marker image for the watershed algorithmMatmarkers = Mat::zeros(dist.size(), CV_32S);
// Draw the foreground markersfor (size_ti = 0; i < contours.size(); i++)
{
drawContours(markers, contours, static_cast<int>(i), Scalar(static_cast<int>(i)+1), -1);
}
// Draw the background markercircle(markers, Point(5,5), 3, Scalar(255), -1);
Matmarkers8u;
markers.convertTo(markers8u, CV_8U, 10);
imshow("Markers", markers8u);
// Perform the watershed algorithmwatershed(imgResult, markers);
Matmark;
markers.convertTo(mark, CV_8U);
bitwise_not(mark, mark);
// imshow("Markers_v2", mark); // uncomment this if you want to see how the mark// image looks like at that point// Generate random colorsvector<Vec3b> colors;
for (size_ti = 0; i < contours.size(); i++)
{
intb = theRNG().uniform(0, 256);
intg = theRNG().uniform(0, 256);
intr = theRNG().uniform(0, 256);
colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
}
// Create the result imageMatdst = Mat::zeros(markers.size(), CV_8UC3);
// Fill labeled objects with random colorsfor (inti = 0; i < markers.rows; i++)
{
for (intj = 0; j < markers.cols; j++)
{
intindex = markers.at<int>(i,j);
if (index > 0 && index <= static_cast<int>(contours.size()))
{
dst.at<Vec3b>(i,j) = colors[index-1];
}
}
}
// Visualize the final imageimshow("Final Result", dst);
waitKey();
return0;
}
Java
This tutorial code's is shown lines below. You can also download it from here
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Random;
importorg.opencv.core.Core;
importorg.opencv.core.CvType;
importorg.opencv.core.Mat;
importorg.opencv.core.MatOfPoint;
importorg.opencv.core.Point;
importorg.opencv.core.Scalar;
importorg.opencv.highgui.HighGui;
importorg.opencv.imgcodecs.Imgcodecs;
importorg.opencv.imgproc.Imgproc;
classImageSegmentation {
publicvoidrun(String[] args) {
// Load the imageStringfilename = args.length > 0 ? args[0] : "../data/cards.png";
MatsrcOriginal = Imgcodecs.imread(filename);
if (srcOriginal.empty()) {
System.err.println("Cannot read image: " + filename);
System.exit(0);
}
// Show source imageHighGui.imshow("Source Image", srcOriginal);
// Change the background from white to black, since that will help later to// extract// better results during the use of Distance TransformMatsrc = srcOriginal.clone();
byte[] srcData = newbyte[(int) (src.total() * src.channels())];
src.get(0, 0, srcData);
for (inti = 0; i < src.rows(); i++) {
for (intj = 0; j < src.cols(); j++) {
if (srcData[(i * src.cols() + j) * 3] == (byte) 255 && srcData[(i * src.cols() + j) * 3 + 1] == (byte) 255
&& srcData[(i * src.cols() + j) * 3 + 2] == (byte) 255) {
srcData[(i * src.cols() + j) * 3] = 0;
srcData[(i * src.cols() + j) * 3 + 1] = 0;
srcData[(i * src.cols() + j) * 3 + 2] = 0;
}
}
}
src.put(0, 0, srcData);
// Show output imageHighGui.imshow("Black Background Image", src);
// Create a kernel that we will use to sharpen our imageMatkernel = newMat(3, 3, CvType.CV_32F);
// an approximation of second derivative, a quite strong kernelfloat[] kernelData = newfloat[(int) (kernel.total() * kernel.channels())];
kernelData[0] = 1; kernelData[1] = 1; kernelData[2] = 1;
kernelData[3] = 1; kernelData[4] = -8; kernelData[5] = 1;
kernelData[6] = 1; kernelData[7] = 1; kernelData[8] = 1;
kernel.put(0, 0, kernelData);
// do the laplacian filtering as it is// well, we need to convert everything in something more deeper then CV_8U// because the kernel has some negative values,// and we can expect in general to have a Laplacian image with negative values// BUT a 8bits unsigned int (the one we are working with) can contain values// from 0 to 255// so the possible negative number will be truncatedMatimgLaplacian = newMat();
Imgproc.filter2D(src, imgLaplacian, CvType.CV_32F, kernel);
Matsharp = newMat();
src.convertTo(sharp, CvType.CV_32F);
MatimgResult = newMat();
Core.subtract(sharp, imgLaplacian, imgResult);
// convert back to 8bits gray scaleimgResult.convertTo(imgResult, CvType.CV_8UC3);
imgLaplacian.convertTo(imgLaplacian, CvType.CV_8UC3);
// imshow( "Laplace Filtered Image", imgLaplacian );HighGui.imshow("New Sharped Image", imgResult);
// Create binary image from source imageMatbw = newMat();
Imgproc.cvtColor(imgResult, bw, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(bw, bw, 40, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);
HighGui.imshow("Binary Image", bw);
// Perform the distance transform algorithmMatdist = newMat();
Imgproc.distanceTransform(bw, dist, Imgproc.DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}// so we can visualize and threshold itCore.normalize(dist, dist, 0.0, 1.0, Core.NORM_MINMAX);
MatdistDisplayScaled = newMat();
Core.multiply(dist, newScalar(255), distDisplayScaled);
MatdistDisplay = newMat();
distDisplayScaled.convertTo(distDisplay, CvType.CV_8U);
HighGui.imshow("Distance Transform Image", distDisplay);
// Threshold to obtain the peaks// This will be the markers for the foreground objectsImgproc.threshold(dist, dist, 0.4, 1.0, Imgproc.THRESH_BINARY);
// Dilate a bit the dist imageMatkernel1 = Mat.ones(3, 3, CvType.CV_8U);
Imgproc.dilate(dist, dist, kernel1);
MatdistDisplay2 = newMat();
dist.convertTo(distDisplay2, CvType.CV_8U);
Core.multiply(distDisplay2, newScalar(255), distDisplay2);
HighGui.imshow("Peaks", distDisplay2);
// Create the CV_8U version of the distance image// It is needed for findContours()Matdist_8u = newMat();
dist.convertTo(dist_8u, CvType.CV_8U);
// Find total markersList<MatOfPoint> contours = newArrayList<>();
Mathierarchy = newMat();
Imgproc.findContours(dist_8u, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
// Create the marker image for the watershed algorithmMatmarkers = Mat.zeros(dist.size(), CvType.CV_32S);
// Draw the foreground markersfor (inti = 0; i < contours.size(); i++) {
Imgproc.drawContours(markers, contours, i, newScalar(i + 1), -1);
}
// Draw the background markerMatmarkersScaled = newMat();
markers.convertTo(markersScaled, CvType.CV_32F);
Core.normalize(markersScaled, markersScaled, 0.0, 255.0, Core.NORM_MINMAX);
Imgproc.circle(markersScaled, newPoint(5, 5), 3, newScalar(255, 255, 255), -1);
MatmarkersDisplay = newMat();
markersScaled.convertTo(markersDisplay, CvType.CV_8U);
HighGui.imshow("Markers", markersDisplay);
Imgproc.circle(markers, newPoint(5, 5), 3, newScalar(255, 255, 255), -1);
// Perform the watershed algorithmImgproc.watershed(imgResult, markers);
Matmark = Mat.zeros(markers.size(), CvType.CV_8U);
markers.convertTo(mark, CvType.CV_8UC1);
Core.bitwise_not(mark, mark);
// imshow("Markers_v2", mark); // uncomment this if you want to see how the mark// image looks like at that point// Generate random colorsRandomrng = newRandom(12345);
List<Scalar> colors = newArrayList<>(contours.size());
for (inti = 0; i < contours.size(); i++) {
intb = rng.nextInt(256);
intg = rng.nextInt(256);
intr = rng.nextInt(256);
colors.add(newScalar(b, g, r));
}
// Create the result imageMatdst = Mat.zeros(markers.size(), CvType.CV_8UC3);
byte[] dstData = newbyte[(int) (dst.total() * dst.channels())];
dst.get(0, 0, dstData);
// Fill labeled objects with random colorsint[] markersData = newint[(int) (markers.total() * markers.channels())];
markers.get(0, 0, markersData);
for (inti = 0; i < markers.rows(); i++) {
for (intj = 0; j < markers.cols(); j++) {
intindex = markersData[i * markers.cols() + j];
if (index > 0 && index <= contours.size()) {
dstData[(i * dst.cols() + j) * 3 + 0] = (byte) colors.get(index - 1).val[0];
dstData[(i * dst.cols() + j) * 3 + 1] = (byte) colors.get(index - 1).val[1];
dstData[(i * dst.cols() + j) * 3 + 2] = (byte) colors.get(index - 1).val[2];
} else {
dstData[(i * dst.cols() + j) * 3 + 0] = 0;
dstData[(i * dst.cols() + j) * 3 + 1] = 0;
dstData[(i * dst.cols() + j) * 3 + 2] = 0;
}
}
}
dst.put(0, 0, dstData);
// Visualize the final imageHighGui.imshow("Final Result", dst);
HighGui.waitKey();
System.exit(0);
}
}
publicclassImageSegmentationDemo {
publicstaticvoidmain(String[] args) {
// Load the native OpenCV librarySystem.loadLibrary(Core.NATIVE_LIBRARY_NAME);
newImageSegmentation().run(args);
}
}
Python
This tutorial code's is shown lines below. You can also download it from here
from__future__importprint_functionimportcv2ascvimportnumpyasnpimportargparseimportrandomasrngrng.seed(12345)
parser = argparse.ArgumentParser(description='CodeforImageSegmentationwithDistanceTransformandWatershedAlgorithm.\
SamplecodeshowinghowtosegmentoverlappingobjectsusingLaplacianfiltering, \
inadditiontoWatershedandDistanceTransformation')
parser.add_argument('--input', help='Pathtoinputimage.', default='cards.png')
args = parser.parse_args()
src = cv.imread(cv.samples.findFile(args.input))
if src is None:
print('Couldnotopenorfindtheimage:', args.input)
exit(0)
# Show source image
cv.imshow('SourceImage', src)
src[np.all(src == 255, axis=2)] = 0
# Show output image
cv.imshow('BlackBackgroundImage', src)
kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]], dtype=np.float32)
# do the laplacian filtering as it is
# well, we need to convert everything in something more deeper then CV_8U
# because the kernel has some negative values,
# and we can expect in general to have a Laplacian image with negative values
# BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
# so the possible negative number will be truncated
imgLaplacian = cv.filter2D(src, cv.CV_32F, kernel)
sharp = np.float32(src)
imgResult = sharp - imgLaplacian
# convert back to 8bits gray scale
imgResult = np.clip(imgResult, 0, 255)
imgResult = imgResult.astype('uint8')
imgLaplacian = np.clip(imgLaplacian, 0, 255)
imgLaplacian = np.uint8(imgLaplacian)
#cv.imshow('LaplaceFilteredImage', imgLaplacian)
cv.imshow('NewSharpedImage', imgResult)
bw = cv.cvtColor(imgResult, cv.COLOR_BGR2GRAY)
_, bw = cv.threshold(bw, 40, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow('BinaryImage', bw)
dist = cv.distanceTransform(bw, cv.DIST_L2, 3)
# Normalize the distance image for range = {0.0, 1.0}
# so we can visualize and threshold it
cv.normalize(dist, dist, 0, 1.0, cv.NORM_MINMAX)
cv.imshow('DistanceTransformImage', dist)
_, dist = cv.threshold(dist, 0.4, 1.0, cv.THRESH_BINARY)
# Dilate a bit the dist image
kernel1 = np.ones((3,3), dtype=np.uint8)
dist = cv.dilate(dist, kernel1)
cv.imshow('Peaks', dist)
dist_8u = dist.astype('uint8')
# Find total markers
contours, _ = cv.findContours(dist_8u, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# Create the marker image for the watershed algorithm
markers = np.zeros(dist.shape, dtype=np.int32)
# Draw the foreground markers
for i in range(len(contours)):
cv.drawContours(markers, contours, i, (i+1), -1)
# Draw the background marker
cv.circle(markers, (5,5), 3, (255,255,255), -1)
markers_8u = (markers * 10).astype('uint8')
cv.imshow('Markers', markers_8u)
cv.watershed(imgResult, markers)
#mark = np.zeros(markers.shape, dtype=np.uint8)
mark = markers.astype('uint8')
mark = cv.bitwise_not(mark)
# uncomment this if you want to see how the mark
# image looks like at that point
#cv.imshow('Markers_v2', mark)
# Generate random colors
colors = []
for contour in contours:
colors.append((rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)))
# Create the result image
dst = np.zeros((markers.shape[0], markers.shape[1], 3), dtype=np.uint8)
# Fill labeled objects with random colors
for i in range(markers.shape[0]):
for j in range(markers.shape[1]):
index = markers[i,j]
if index > 0 and index <= len(contours):
dst[i,j,:] = colors[index-1]
# Visualize the final image
cv.imshow('FinalResult', dst)
cv.waitKey()
Explanation / Result
Load the source image and check if it is loaded without any problem, then show it:
C++
// Load the imageCommandLineParserparser( argc, argv, "{@input | cards.png | input image}" );
Matsrc = imread( samples::findFile( parser.get<String>( "@input" ) ) );
if( src.empty() )
{
cout << "Could not open or find the image!\n" << endl;
cout << "Usage: " << argv[0] << " <Input image>" << endl;
return -1;
}
// Show the source imageimshow("Source Image", src);
parser = argparse.ArgumentParser(description='CodeforImageSegmentationwithDistanceTransformandWatershedAlgorithm.\
SamplecodeshowinghowtosegmentoverlappingobjectsusingLaplacianfiltering, \
inadditiontoWatershedandDistanceTransformation')
parser.add_argument('--input', help='Pathtoinputimage.', default='cards.png')
args = parser.parse_args()
src = cv.imread(cv.samples.findFile(args.input))
if src is None:
print('Couldnotopenorfindtheimage:', args.input)
exit(0)
# Show source image
cv.imshow('SourceImage', src)
Then if we have an image with a white background, it is good to transform it to black. This will help us to discriminate the foreground objects easier when we will apply the Distance Transform:
C++
// Change the background from white to black, since that will help later to extract// better results during the use of Distance TransformMatmask;
inRange(src, Scalar(255, 255, 255), Scalar(255, 255, 255), mask);
src.setTo(Scalar(0, 0, 0), mask);
// Show output imageimshow("Black Background Image", src);
Java
// Change the background from white to black, since that will help later to// extract// better results during the use of Distance TransformMatsrc = srcOriginal.clone();
byte[] srcData = newbyte[(int) (src.total() * src.channels())];
src.get(0, 0, srcData);
for (inti = 0; i < src.rows(); i++) {
for (intj = 0; j < src.cols(); j++) {
if (srcData[(i * src.cols() + j) * 3] == (byte) 255 && srcData[(i * src.cols() + j) * 3 + 1] == (byte) 255
&& srcData[(i * src.cols() + j) * 3 + 2] == (byte) 255) {
srcData[(i * src.cols() + j) * 3] = 0;
srcData[(i * src.cols() + j) * 3 + 1] = 0;
srcData[(i * src.cols() + j) * 3 + 2] = 0;
}
}
}
src.put(0, 0, srcData);
// Show output imageHighGui.imshow("Black Background Image", src);
Afterwards we will sharpen our image in order to acute the edges of the foreground objects. We will apply a laplacian filter with a quite strong filter (an approximation of second derivative):
C++
// Create a kernel that we will use to sharpen our imageMatkernel = (Mat_<float>(3,3) <<
1, 1, 1,
1, -8, 1,
1, 1, 1); // an approximation of second derivative, a quite strong kernel// do the laplacian filtering as it is// well, we need to convert everything in something more deeper then CV_8U// because the kernel has some negative values,// and we can expect in general to have a Laplacian image with negative values// BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255// so the possible negative number will be truncatedMatimgLaplacian;
filter2D(src, imgLaplacian, CV_32F, kernel);
Matsharp;
src.convertTo(sharp, CV_32F);
MatimgResult = sharp - imgLaplacian;
// convert back to 8bits gray scaleimgResult.convertTo(imgResult, CV_8UC3);
imgLaplacian.convertTo(imgLaplacian, CV_8UC3);
// imshow( "Laplace Filtered Image", imgLaplacian );imshow( "New Sharped Image", imgResult );
Java
// Create a kernel that we will use to sharpen our imageMatkernel = newMat(3, 3, CvType.CV_32F);
// an approximation of second derivative, a quite strong kernelfloat[] kernelData = newfloat[(int) (kernel.total() * kernel.channels())];
kernelData[0] = 1; kernelData[1] = 1; kernelData[2] = 1;
kernelData[3] = 1; kernelData[4] = -8; kernelData[5] = 1;
kernelData[6] = 1; kernelData[7] = 1; kernelData[8] = 1;
kernel.put(0, 0, kernelData);
// do the laplacian filtering as it is// well, we need to convert everything in something more deeper then CV_8U// because the kernel has some negative values,// and we can expect in general to have a Laplacian image with negative values// BUT a 8bits unsigned int (the one we are working with) can contain values// from 0 to 255// so the possible negative number will be truncatedMatimgLaplacian = newMat();
Imgproc.filter2D(src, imgLaplacian, CvType.CV_32F, kernel);
Matsharp = newMat();
src.convertTo(sharp, CvType.CV_32F);
MatimgResult = newMat();
Core.subtract(sharp, imgLaplacian, imgResult);
// convert back to 8bits gray scaleimgResult.convertTo(imgResult, CvType.CV_8UC3);
imgLaplacian.convertTo(imgLaplacian, CvType.CV_8UC3);
// imshow( "Laplace Filtered Image", imgLaplacian );HighGui.imshow("New Sharped Image", imgResult);
We are ready now to apply the Distance Transform on the binary image. Moreover, we normalize the output image in order to be able visualize and threshold the result:
C++
// Perform the distance transform algorithmMatdist;
distanceTransform(bw, dist, DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}// so we can visualize and threshold itnormalize(dist, dist, 0, 1.0, NORM_MINMAX);
imshow("Distance Transform Image", dist);
Java
// Perform the distance transform algorithmMatdist = newMat();
Imgproc.distanceTransform(bw, dist, Imgproc.DIST_L2, 3);
// Normalize the distance image for range = {0.0, 1.0}// so we can visualize and threshold itCore.normalize(dist, dist, 0.0, 1.0, Core.NORM_MINMAX);
MatdistDisplayScaled = newMat();
Core.multiply(dist, newScalar(255), distDisplayScaled);
MatdistDisplay = newMat();
distDisplayScaled.convertTo(distDisplay, CvType.CV_8U);
HighGui.imshow("Distance Transform Image", distDisplay);
We threshold the dist image and then perform some morphology operation (i.e. dilation) in order to extract the peaks from the above image:
C++
// Threshold to obtain the peaks// This will be the markers for the foreground objectsthreshold(dist, dist, 0.4, 1.0, THRESH_BINARY);
// Dilate a bit the dist imageMatkernel1 = Mat::ones(3, 3, CV_8U);
dilate(dist, dist, kernel1);
imshow("Peaks", dist);
Java
// Threshold to obtain the peaks// This will be the markers for the foreground objectsImgproc.threshold(dist, dist, 0.4, 1.0, Imgproc.THRESH_BINARY);
// Dilate a bit the dist imageMatkernel1 = Mat.ones(3, 3, CvType.CV_8U);
Imgproc.dilate(dist, dist, kernel1);
MatdistDisplay2 = newMat();
dist.convertTo(distDisplay2, CvType.CV_8U);
Core.multiply(distDisplay2, newScalar(255), distDisplay2);
HighGui.imshow("Peaks", distDisplay2);
Finally, we can apply the watershed algorithm, and visualize the result:
C++
// Perform the watershed algorithmwatershed(imgResult, markers);
Matmark;
markers.convertTo(mark, CV_8U);
bitwise_not(mark, mark);
// imshow("Markers_v2", mark); // uncomment this if you want to see how the mark// image looks like at that point// Generate random colorsvector<Vec3b> colors;
for (size_ti = 0; i < contours.size(); i++)
{
intb = theRNG().uniform(0, 256);
intg = theRNG().uniform(0, 256);
intr = theRNG().uniform(0, 256);
colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
}
// Create the result imageMatdst = Mat::zeros(markers.size(), CV_8UC3);
// Fill labeled objects with random colorsfor (inti = 0; i < markers.rows; i++)
{
for (intj = 0; j < markers.cols; j++)
{
intindex = markers.at<int>(i,j);
if (index > 0 && index <= static_cast<int>(contours.size()))
{
dst.at<Vec3b>(i,j) = colors[index-1];
}
}
}
// Visualize the final imageimshow("Final Result", dst);
Java
// Perform the watershed algorithmImgproc.watershed(imgResult, markers);
Matmark = Mat.zeros(markers.size(), CvType.CV_8U);
markers.convertTo(mark, CvType.CV_8UC1);
Core.bitwise_not(mark, mark);
// imshow("Markers_v2", mark); // uncomment this if you want to see how the mark// image looks like at that point// Generate random colorsRandomrng = newRandom(12345);
List<Scalar> colors = newArrayList<>(contours.size());
for (inti = 0; i < contours.size(); i++) {
intb = rng.nextInt(256);
intg = rng.nextInt(256);
intr = rng.nextInt(256);
colors.add(newScalar(b, g, r));
}
// Create the result imageMatdst = Mat.zeros(markers.size(), CvType.CV_8UC3);
byte[] dstData = newbyte[(int) (dst.total() * dst.channels())];
dst.get(0, 0, dstData);
// Fill labeled objects with random colorsint[] markersData = newint[(int) (markers.total() * markers.channels())];
markers.get(0, 0, markersData);
for (inti = 0; i < markers.rows(); i++) {
for (intj = 0; j < markers.cols(); j++) {
intindex = markersData[i * markers.cols() + j];
if (index > 0 && index <= contours.size()) {
dstData[(i * dst.cols() + j) * 3 + 0] = (byte) colors.get(index - 1).val[0];
dstData[(i * dst.cols() + j) * 3 + 1] = (byte) colors.get(index - 1).val[1];
dstData[(i * dst.cols() + j) * 3 + 2] = (byte) colors.get(index - 1).val[2];
} else {
dstData[(i * dst.cols() + j) * 3 + 0] = 0;
dstData[(i * dst.cols() + j) * 3 + 1] = 0;
dstData[(i * dst.cols() + j) * 3 + 2] = 0;
}
}
}
dst.put(0, 0, dstData);
// Visualize the final imageHighGui.imshow("Final Result", dst);