链接图片-> https://pbs.twimg.com/media/D4dubkYWkBgojS4.png
我尝试进行相关的转换,但是在尝试检测肾脏肿瘤时的结果不是预期的。...我将非常有帮助的答案...非常感谢。
我正在尝试使用opencvsharp选择肾脏的肿瘤区域 在图像中,我显示了从图像中获得的结果,并用红色标记了我想要获得的真实区域
var COMP = new Mat();
var src = new Mat(openFileDialog1.FileName, ImreadModes.AnyDepth | ImreadModes.AnyColor);
src.CopyTo(COMP);
var srcCopy = new Mat();
var fg = new Mat();
var bgt = new Mat();
src.CopyTo(srcCopy);
var markerMask = new Mat();
var mar = new Mat();
Cv2.CvtColor(srcCopy, markerMask, ColorConversionCodes.BGRA2GRAY);
Cv2.EqualizeHist(markerMask, markerMask);
Cv2.ImShow("dede1", markerMask);
Cv2.Threshold(markerMask, markerMask, 2,255, ThresholdTypes.Binary | ThresholdTypes.Otsu );
Cv2.ImShow("dede2", markerMask);
Cv2.Erode(markerMask, fg, null, null, 2);
Cv2.Dilate(markerMask, bgt, null, null, 3);
Cv2.Threshold(bgt, bgt, 2, 128, ThresholdTypes.BinaryInv);
bgt = bgt + fg;
bgt.CopyTo(mar);
Cv2.CvtColor(mar, mar, ColorConversionCodes.GRAY2BGR);
COMP.Add(mar);
////////////////////////////////////////
Point[][] contours; //vector<vector<Point>> contours;
HierarchyIndex[] hierarchyIndexes; //vector<Vec4i> hierarchy;
Cv2.FindContours(
bgt,
out contours,
out hierarchyIndexes,
mode: RetrievalModes.CComp,
method: ContourApproximationModes.ApproxSimple);
var markers = new Mat(bgt.Size(), MatType.CV_32S, s: Scalar.All(0));
var componentCount = 0;
var contourIndex = 0;
while ((contourIndex >= 0))
{
Cv2.DrawContours(
markers,
contours,
contourIndex,
color: Scalar.All(componentCount + 1),
thickness: -1,
lineType: LineTypes.Link8,
hierarchy: hierarchyIndexes,
maxLevel: int.MaxValue);
componentCount++;
contourIndex = hierarchyIndexes[contourIndex].Next;
}
var rnd = new Random();
var colorTable = new List<Vec3b>();
for (var i = 0; i < componentCount; i++)
{
var b = rnd.Next(0, 255); //Cv2.TheRNG().Uniform(0, 255);
var g = rnd.Next(0, 255); //Cv2.TheRNG().Uniform(0, 255);
var r = rnd.Next(0, 255); //Cv2.TheRNG().Uniform(0, 255);
colorTable.Add(new Vec3b((byte)b, (byte)g, (byte)r));
}
Cv2.Watershed(src, markers);
var watershedImage = new Mat(markers.Size(), MatType.CV_8UC3);
// paint the watershed image
for (var i = 0; i < markers.Rows; i++)
{
for (var j = 0; j < markers.Cols; j++)
{
var idx = markers.At<int>(i, j);
if (idx == -1)
{
watershedImage.Set(i, j, new Vec3b(255, 255, 255));
}
else if (idx <= 0 || idx > componentCount)
{
watershedImage.Set(i, j, new Vec3b(0, 0, 0));
}
else
{
watershedImage.Set(i, j, colorTable[idx - 1]);
}
}
}
var imgGray = new Mat();
Cv2.CvtColor(markerMask, imgGray, ColorConversionCodes.GRAY2BGR);
watershedImage = watershedImage * 0.5 + imgGray * 0.5;
COMP.Add(watershedImage);
Cv2.CvtColor(watershedImage, watershedImage, ColorConversionCodes.RGB2GRAY);
Cv2.Threshold(watershedImage, watershedImage, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
// COMP.Add(watershedImage);
Cv2.ImWrite("D:\\ima1.JPG", COMP);
Cv2.ImWrite("D:\\IM1.PNG", watershedImage);
var aux1 = new Mat();
watershedImage.CopyTo(aux1);
for (var i = 0; i < markers.Rows; i++)
{
for (var j = 0; j < markers.Cols; j++)
{
var idx = markers.At<int>(i, j);
if (idx == -1)
{
watershedImage.Set(i, j, new Vec3b(0, 255, 255));
}
else if (idx <= 0 || idx > componentCount)
{
watershedImage.Set(i, j, new Vec3b(0, 255, 255));
}
else
{
watershedImage.Set(i, j, new Vec3b(255, 0, 0));
}
}
}
Cv2.ImShow("Watershed Transform 2", watershedImage);
Cv2.ImWrite("D:\\trnas.png", watershedImage);
// aux1 = aux1.GreaterThanOrEqual(watershedImage);
// Cv2.CvtColor(watershedImage, watershedImage, ColorConversionCodes.BGRA2GRAY);
// Cv2.Threshold(watershedImage, watershedImage, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
Cv2.ImShow("Watershed Transform", aux1);
Cv2.WaitKey(100000); //do eventsenter image description here