I'm trying to create a histogram for Back Projection of an image using the Emgu C# wrapper for OpenCV. I have the following OpenCV C++ code which I am trying to convert to Emgu C#:
char* filename = (char*)"C:\\Images\\items.jpg";
Mat im = imread(filename);
if (im.empty())
return -1;
const int channels[] = { 0, 1, 2 };
const int histSize[] = { 32, 32, 32 };
const float rgbRange[] = { 0, 256 };
const float* ranges[] = { rgbRange, rgbRange, rgbRange };
Mat hist;
Mat im32fc3, backpr32f;
im.convertTo(im32fc3, CV_32FC3);
calcHist(&im32fc3, 1, channels, Mat(), hist, 3, histSize, ranges, true, false);
calcBackProject(&im32fc3, 1, channels, hist, backpr32f, ranges);
Emgu C# conversion:
string filename = #"C:\Images\items.jpg";
Mat im = CvInvoke.Imread(filename);
if (im.IsEmpty)
{
return -1;
}
int[] channels = { 0, 1, 2 };
int[] histSize = { 32, 32, 32 };
float[] ranges = { 0.0f, 256.0f, 0.0f, 256.0f, 0.0f, 256.0f };
Mat im32fc3 = new Mat();
Mat hist = new Mat();
Mat mask = new Mat();
Mat backpr32f = new Mat();
im.ConvertTo(im32fc3, Emgu.CV.CvEnum.DepthType.Cv32F);
CvInvoke.CalcHist(im32fc3, channels, mask, hist, histSize, ranges, false);
CvInvoke.CalcBackProject(im32fc3, channels, hist, backpr32f, ranges);
However, I get the following error at CvInvoke.CalcHist:
OpenCV: 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows
Any help is appreciated.
I had the same problem. found this piece of code in emgu VS solution.
using (Util.VectorOfMat vm = new Util.VectorOfMat())
{
vm.Push(channels[i]);
float[] ranges = new float[] { minVal, maxVal };
CvInvoke.CalcHist(vm, new int[] { 0 }, null, hist, new int[] { numberOfBins }, ranges, false);
}
use VectorOfMat for 1st arugment instead of Mat.
Just wrap the Mat into a vector(array in c/c++) then pass it the first argument of CalcHist():
using var im32fc3Vector = new VectorOfMat(im32fc3);
CvInvoke.CalcHist(im32fc3Vector, ...);
Related
The CvInvoke.PCACompute method expects a IInputArray of data, to do the analysis.
I tried using the source image as the input Mat, but the eigenvectors computed are abnormal, as per my understanding. And I am not able to convert my Contour VectorOfPoint to Mat, which can me fed.
I could also not find a good literature online about implementing PCA Analysis in EmguCV / C#.
Can someone please point me in the right direction.
Below is my code -
public static void getOrientation(Image<Gray,byte> inputImage)
{
Image<Gray, Byte> cannyGray = inputImage.Canny(85, 255);
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
Mat eigen_vectors = new Mat(inputImage.Size,DepthType.Cv8U,1);
Mat mean_mat = new Mat(inputImage.Size, DepthType.Cv8U, 1);
CvInvoke.FindContours(cannyGray, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple);
Point[][] cont_points = contours.ToArrayOfArray();
Mat contour_mat = new Mat();
contour_mat.SetTo(cont_points[0]);
//CvInvoke.PCACompute(cannyGray.Mat, mean_mat, eigen_vectors,2);
CvInvoke.PCACompute(contours, mean_mat, eigen_vectors);
}
You have to convert each of your contour to a Mat containing your coordinates.
Here is an example of how you can do it:
// points are the point of one contour
var pointList = points.ToArray();
// use DepthType.Cv64F to allow numbers > 255
Mat dataPoints = new Mat(pointList.Length, 2, DepthType.Cv64F, 1);
double[] pointsData = new double[((int)dataPoints.Total * dataPoints.NumberOfChannels)];
// store the points coordinates in the Mat
for (int i = 0; i < dataPoints.Rows; i++)
{
pointsData[i * dataPoints.Cols] = pointList[i].X;
pointsData[i * dataPoints.Cols + 1] = pointList[i].Y;
}
// set the Mat to dataPointsData values
dataPoints.SetTo(pointsData);
// compute PCA
Mat mean = new Mat();
Mat eigenvectors = new Mat();
Mat eigenvalues = new Mat();
CvInvoke.PCACompute(dataPoints, mean, eigenvectors);
I am working on a project in which I try to compare two images in C#. I'm using EmguCV (a C# wrapper for OpenCV). I've tested some funcitons which work (compareHist for example).
I am now trying to use the implementation of Earth's Mover Distance. As I use color images, I build a 2d Histogram based on the HSV image. I then build the corresponding signature (as described in the docs, and explained here).
The problem is that I always obtain a NaN as the output of my code. Since I'm new to C# and EmguCV, I've tried to make the same steps, but this time in Python, and it works, EMD returns a number without any error.
I've spent a lot of time on this problem trying to change the type of the histogram (between the OpenCV Mat and EmguCV image), looking at the histograms values to verify if they are right,... But I don't find what I'm doing wrong.
About the code:
I have a "Comparator" class which just contain 2 images:
class Comparator
{
public Image<Bgr, Byte> RefImage;
public Image<Bgr, Byte> TestImage;
public Comparator(string TestPath, string RefPath)
{
Image<Bgr, Byte> TestImagetemp = new Image<Bgr, Byte>(TestPath);
Image<Bgr, Byte> RefImagetemp = new Image<Bgr, Byte>(RefPath);
int newCols = Math.Min(TestImagetemp.Cols, RefImagetemp.Cols);
int newRows = Math.Min(RefImagetemp.Rows, TestImagetemp.Rows);
Rectangle roi = new Rectangle(0, 0, newCols, newRows);
this.RefImage = crop(RefImagetemp, roi);
this.TestImage = crop(TestImagetemp, roi);
string DiffPath = "C:\\Users\\EPIERSO\\Docs\\testdiff";
this.TestImage.Save(DiffPath + "testavant.png");
}
Here is the method used for computing the histogram:
public static Mat CalcHistHSV(Image<Bgr,Byte> image)
{
int[] histbins = new int[] { 30, 32 };
float[] ranges = new float[] { 0.0f, 180.0f, 0.0f, 256.0f };
Mat hist = new Mat();
VectorOfMat vm = new VectorOfMat();
Image<Hsv,float> imghsv = image.Convert<Hsv, float>();
vm.Push(imghsv);
CvInvoke.CalcHist(vm, new int[] { 0, 1 }, null, hist, histbins, ranges, false);
return hist;
}
And this is the method used for comparing with EMD:
public bool EMDCompare()
{
int hbins = 30;
int sbins = 32;
Mat histref = CalcHistHSV(RefImage);
Mat histtest = CalcHistHSV(TestImage);
//Computing the signatures
Mat sigref = new Mat(hbins*sbins,3,Emgu.CV.CvEnum.DepthType.Cv32F,1);
Mat sigtest = new Mat(hbins*sbins,3, Emgu.CV.CvEnum.DepthType.Cv32F, 1);
for (int h = 0; h<hbins; h++)
{
for (int s = 0; s < sbins; s++)
{
var bin = MatExtension.GetValue(histref,h,s);
MatExtension.SetValue(sigref, h * sbins + s, 0, bin);
MatExtension.SetValue(sigref, h * sbins + s, 1, h);
MatExtension.SetValue(sigref, h * sbins + s, 2, s);
var bin2 = MatExtension.GetValue(histtest, h, s);
MatExtension.SetValue(sigtest, h * sbins + s, 0, bin2);
MatExtension.SetValue(sigtest, h * sbins + s, 1, h);
MatExtension.SetValue(sigtest, h * sbins + s, 2, s);
}
}
float emd = CvInvoke.EMD(sigref, sigtest, DistType.L2);
return ((1 - emd) > 0.7);
}
For modifying Mat values, I use an extension named MatExtension, found here: How can I get and set pixel values of an EmguCV Mat image?
This is the equivalent Python code: https://pastebin.com/drhvNMNs
I'm trying to calculate an histogram from an image with OpenCV for C# (EmguCV).
This is my code:
VectorOfMat bgr_planes = new VectorOfMat();
CvInvoke.Split(myImage, bgr_planes);
int[] dim = new int[] { 0 };
int[] histSize = new int[] { 256 };
float[] range = new float[] { 0f, 255f };
bool accumulate = false;
Mat b_hist = new Mat();
Mat g_hist = new Mat();
Mat r_hist = new Mat();
CvInvoke.CalcHist(bgr_planes, dim, new Mat(), b_hist, histSize, range, accumulate);
CvInvoke.CalcHist(bgr_planes, dim, new Mat(), g_hist, histSize, range, accumulate);
CvInvoke.CalcHist(bgr_planes, dim, new Mat(), r_hist, histSize, range, accumulate);`
But I do not get Data b_hist, g_hist and r_hist. I can't address the specific channels bgr_planes[0], because those are of type Mat and it throws an error.
How can I adjust the parameters dim, histSize and range to get a color histogram?
Thank you so much for your help!
I solved it myself (of course right after posting the question...):
Here is what I changed:
Mat x = bgr_planes[0];
Mat[] bVals = new Mat[1] { x };
VectorOfMat colorChannelB = new VectorOfMat();
colorChannelB.Push(bVals);
This separates one single color channel of the bgr_planes. In VectorOfMat colorChannelB is now only one color channel. Do that separately for every channel and keep the parameters.
With the function below, I'm running into problems trying to access the WriteableBitmap.PixelBuffer property. The message I am getting is:
'WriteableBitmap' does not contain a definition for 'PixelBuffer' and no extension method 'PixelBuffer' accepting a first argument of type 'WriteableBitmap' could be found (are you missing a using directive or an assembly reference?)
I have read other places that I need to include
using System.Runtime.InteropServices.WindowsRuntime;
But when I use this include, nothing changes in my code. Looking through the references of my solution, I don't see anything like System.Runtime.InteropServices. Im frustrated as this seems to be the solution to other people trying to access the PixelBuffer of a WriteableBitmap.
private WriteableBitmap ChangeBrightness(WriteableBitmap source, byte change_value)
{
WriteableBitmap dest = new WriteableBitmap(source);
byte[] color = new byte[4];
using (Stream s = source.PixelBuffer.AsStream())
{
using (Stream d = dest.PixelBuffer.AsStream())
{
// read the pixel color
while (s.Read(color, 0, 4) > 0)
{
// color[0] = b
// color[1] = g
// color[2] = r
// color[3] = a
// do the adding algo per byte (skip the alpha)
for (int i = 0; i < 4; i++)
{
if ((int)color[i] + change_value > 255) color[i] = 255; else color[i] = (byte)(color[i] + change_value);
}
// write the new pixel color
d.Write(color, 0, 4);
}
}
}
// return the new bitmap
return dest;
}
Make sure you are referencing the assembly that package belongs to:
System.Runtime.WindowsRuntime assembly
I've ended up going about solving my original problem via other means. To adjust the brightness of my image, I ended up using this functions instead:
public ImageSource AdjustBrightness(BitmapImage Image, int Value, int mod)
{
Bitmap TempBitmap = BitmapImage2Bitmap(Image);
Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height);
Graphics NewGraphics = Graphics.FromImage(NewBitmap);
float FinalValue = (float)Value / 255.0f;
float[][] FloatColorMatrix ={
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {mod * FinalValue, mod * FinalValue, mod * FinalValue, 1, 1}
};
ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix);
ImageAttributes Attributes = new ImageAttributes();
Attributes.SetColorMatrix(NewColorMatrix);
NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes);
Attributes.Dispose();
NewGraphics.Dispose();
return Bitmap2BitmapImage(NewBitmap);
}
I trying to identify text in an image.
Actually I'm trying to identify the text positions in the image then convert it to text.
I found a some code written on c++ and I am trying to convert it to c#.
Can you help me please?
Extracting text OpenCV
std::vector<cv::Rect> detectLetters(cv::Mat img)
{ std::vector<cv::Rect> boundRect;[enter image description here][1]
cv::Mat img_gray, img_sobel, img_threshold, element;
cvtColor(img, img_gray, CV_BGR2GRAY);
cv::Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
cv::threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU+CV_THRESH_BINARY);
element = getStructuringElement(cv::MORPH_RECT, cv::Size(10, 15) );
cv::morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); //Does the trick
std::vector< std::vector< cv::Point> > contours;
cv::findContours(img_threshold, contours, 0, 1);
std::vector<std::vector<cv::Point> > contours_poly( contours.size() );
for( int i = 0; i < contours.size(); i++ )
if (contours[i].size()>80)
{
cv::approxPolyDP( cv::Mat(contours[i]), contours_poly[i], 17, true );
cv::Rect appRect( boundingRect( cv::Mat(contours_poly[i]) ));
if (appRect.width>appRect.height)
boundRect.push_back(appRect);
}
return boundRect;
}
and i try to convert it to c#, but it didn't work
private List<Rectangle> detectLetters(IntPtr img)
{
//cvtColor(img, img_gray, CV_BGR2GRAY);
List<Rectangle> boundRect = new List<Rectangle>();
//cv::Mat img_gray, img_sobel, img_threshold, element;
IntPtr
img_gray = IntPtr.Zero,
img_sobel= IntPtr.Zero,
img_threshold= IntPtr.Zero,
img_tmp = IntPtr.Zero,
element= IntPtr.Zero;
//cvtColor(img, img_gray, CV_BGR2GRAY);
CvInvoke.cvCvtColor(img, img_gray,COLOR_CONVERSION.CV_BGR2GRAY); //CV_BGR2GRAY);
//cv::Sobel(img_gray, img_sobel, CV_8U, 1, 0, 3, 1, 0, cv::BORDER_DEFAULT);
CvInvoke.cvSobel(img_gray, img_sobel, 0, 1, 1);//, 3, 1, 0, cv.BORDER_DEFAULT);
//cv.threshold(img_sobel, img_threshold, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY);
CvInvoke.cvThreshold(img_sobel, img_threshold, 0, 255, THRESH.CV_THRESH_BINARY|THRESH.CV_THRESH_OTSU);
//element = getStructuringElement(cv.MORPH_RECT, cv.Size(10, 15));
element = CvInvoke.cvCreateStructuringElementEx(1,1,10,15,CV_ELEMENT_SHAPE.CV_SHAPE_RECT,element);// GetStructuringElement(
//cv.morphologyEx(img_threshold, img_threshold, CV_MOP_CLOSE, element); //Does the trick
CvInvoke.cvMorphologyEx(img_threshold,img_threshold,img_tmp,element,CV_MORPH_OP.CV_MOP_CLOSE,1);
//List<List<cv.Point>> contours = new List<List<cv.Point>>();
var contours = new List<IntPtr>();
//cv.findContours(img_threshold, contours, 0, 1);
CvInvoke.cvFindContours(img_threshold, element,ref ((IntPtr)contours[0]), 1, RETR_TYPE.CV_RETR_EXTERNAL, CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE,new Point(0,0));
//std::vector<std::vector<cv::Point> > contours_poly( contours.size() );
var contours_poly = new List<List<Point>>(contours.Count);
//for( int i = 0; i < contours.size(); i++ )
for (int i = 0; i < contours.Count; i++)
{
//if (contours[i].size()>80)
if (contours[i].ToInt32() > 80)
{
//cv.approxPolyDP(Emgu.CV.Matrix<>(contours[i]), contours_poly[i], 17, true);
CvInvoke.cvApproxPoly(contours[i], 17,contours_poly[i],APPROX_POLY_TYPE.CV_POLY_APPROX_DP, 1,1);
//cv::Rect appRect( boundingRect( cv::Mat(contours_poly[i]) ));
Rectangle appRect = new Rectangle(CvInvoke.cvBoundingRect(contours_poly[i],false));
//if (appRect.width>appRect.height)
if (appRect.width > appRect.height)
{
//boundRect.push_back(appRect);
boundRect.Add(appRect);
}
}
}
//return boundRect;
return boundRect;
}
Depending on what the size of the text you're looking for, you may have to play around with the variables for element size and ApproxPolyDP but this code is pretty close to the original but in OpenCvSharp lingo.
static List<Rect> RunTextRecog(string inFile)
{
List<Rect> boundRect = new List<Rect>();
using (Mat img = new Mat(inFile))
using (Mat img_gray = new Mat())
using (Mat img_sobel = new Mat())
using (Mat img_threshold = new Mat())
{
Cv2.CvtColor(img, img_gray, ColorConversionCodes.BGR2GRAY);
Cv2.Sobel(img_gray, img_sobel, MatType.CV_8U, 1, 0, 3, 1, 0, BorderTypes.Default);
Cv2.Threshold(img_sobel, img_threshold, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary);
using (Mat element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(10, 15)))
{
Cv2.MorphologyEx(img_threshold, img_threshold, MorphTypes.Close, element);
Point[][] edgesArray = img_threshold.Clone().FindContoursAsArray(RetrievalModes.External, ContourApproximationModes.ApproxNone);
foreach (Point[] edges in edgesArray)
{
Point[] normalizedEdges = Cv2.ApproxPolyDP(edges, 17, true);
Rect appRect = Cv2.BoundingRect(normalizedEdges);
boundRect.Add(appRect);
}
}
}
return boundRect;
}