Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

CNN segmentation using Caffe, OpenCV and C#

I have a project in C# where I need to use image segmentation. I learned about DNN and OpenCV mostly from pyimagesearch.com (Thanks Adrian! Great site!) However, I do not understand how should I extract individual segments from the result. Here is the part of the code in python:

blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (1024, 512), 0, swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
output = net.forward()
end = time.time()
# infer the total number of classes along with the spatial
# dimensions of the mask image via the shape of the output array
(numClasses, height, width) = output.shape[1:4]
# our output class ID map will be num_classes x height x width in
# size, so we take the argmax to find the class label with the
# largest probability for each and every (x, y)-coordinate in the
# image
classMap = np.argmax(output[0], axis=0)
# given the class ID map, we can map each of the class IDs to its
# corresponding color
mask = COLORS[classMap]
# resize the mask such that its dimensions match the original size
# of the input frame
mask = cv2.resize(mask, (frame.shape[1], frame.shape[0]), interpolation=cv2.INTER_NEAREST)
# perform a weighted combination of the input frame with the mask
# to form an output visualization
output = ((0.3 * frame) + (0.7 * mask)).astype("uint8")

I'm able to create the net, load the .caffemodel, create blob and run "forward()" But I do not know how to write an equivalent of

(numClasses, height, width) = output.shape[1:4]
classMap = np.argmax(output[0], axis=0)
mask = COLORS[classMap]

in C# (I'm using EMGU wrapper for OpenCV)

And here is my code:

Emgu.CV.Dnn.Net segNet = Emgu.CV.Dnn.DnnInvoke.ReadNetFromCaffe("segmentation_deploy.prototxt", "fcn8s-heavy-pascal.caffemodel");
Mat rawImage = Emgu.CV.CvInvoke.Imread(_hideImagePath, ImreadModes.AnyColor);
double scale = 1/255;
Mat blob = Emgu.CV.Dnn.DnnInvoke.BlobFromImage(rawImage, scale, rawImage.Size, new MCvScalar(104.00698793, 116.66876762, 122.67891434), true, false);
segNet.SetInput(blob);
Mat results = segNet.Forward();
Mat tmpMat = results.Reshape(0, results.SizeOfDimension[2]);
ImageViewer vTL1 = new ImageViewer(tmpMat, "tmpMat");
vTL1.Show();

It shows only wide black rectangle. Thanks in advance for any help.

question from:https://stackoverflow.com/questions/65875027/cnn-segmentation-using-caffe-opencv-and-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...