Home > Software engineering >  Matlab and opencv image do bad results
Matlab and opencv image do bad results

Time:09-26

Two pictures do poor white area is extracted, written in matlab before the feelings do not have what problem, the effect is ideal, recently started learning opencv, as also achieve the function of the matlab have done this, but have a few problems, still hope the great god to solve! Feeling is opencv image storage can't directly do bad?
F1 picture

Bg picture

The matlab results

Opencv processing result


Matlab code
Bg=imread (' bg. JPG);
F1=imread (' f1. JPG);
Bg=rgb2gray (bg);
F1=rgb2gray (f1);
Short=f1 - bg.
Imshow (short);

Opencv code
The import cv2
The import numpy as np
Bg=cv2. Imread (" bg. JPG ")
F1=cv2. Imread (" f1. JPG ")
Bg=cv2. CvtColor (bg, cv2 COLOR_BGR2GRAY)
F1=cv2. CvtColor (f1, cv2 COLOR_BGR2GRAY)
Short=bg - f1
Cv2. NamedWindow (' test 'cv2. WINDOW_NORMAL)
Cv2. Imshow (" test ", short)
Cv2. WaitKey (0)

CodePudding user response:

Matlab to set up error, opencv should be accurate

CodePudding user response:

Fyi:
 # include "opencv2/highgui/highgui. HPP" 
# include "opencv2/imgproc/imgproc HPP"
# include "opencv2/imgproc/imgproc_c. H"
using namespace std;
Using the namespace CV;
Mat img, smallImg, gray, bw;
VectorVector Contours.
Int threshval=128;
The Rect r;
The Rect maxrect brect;
Int independence idx, n;
Const static Scalar colors [15]={
CV_RGB (0, 0128),
CV_RGB (0128, 0),
CV_RGB (0128128),
CV_RGB (128, 0, 0),
CV_RGB (128, 0128),
CV_RGB (128128, 0),
CV_RGB (128128128),
CV_RGB (160160160),
CV_RGB (0, 0255),
CV_RGB (0255, 0),
CV_RGB (0255255),
CV_RGB (255, 0, 0),
CV_RGB (255, 0255),
CV_RGB (255255, 0),
CV_RGB (255255255),
};
Scalar color;
Void gamma_correct (Mat& Img, Mat& DST, double gamma) {
Mat temp.
CvMat TMP.

Img. ConvertTo (temp, CV_32FC1, 1.0/255.0, 0.0);
TMP=temp;
CvPow (& amp; TMP, & amp; TMP, gamma);
Temp. ConvertTo (DST, CV_8UC1, 255.0, 0.0);
}
Int main () {
CvNamedWindow (" display ", 1);
Img=imread (" image. JPG ", 1);
R.x=img. Cols/10;
R.y=img. Rows/3;
Truly idth=img. Cols * 8/10;
R.h. eight=img. Rows * 2/3;
SmallImg=img (r);
CvtColor (smallImg, gray, CV_BGR2GRAY);
//medianBlur (gray, gray, 5);
EqualizeHist (gray, gray);
Gamma_correct (gray, gray, 4.0);
Imshow (" display ", gray);
waitKey(0);

Bw=(gray> Threshval);
Imshow (" display ", bw);
waitKey(0);

Mat Structure0=getStructuringElement (MORPH_RECT, Size (3, 3));
Erode (bw, bw, Structure0, Point (1, 1));
Mat Structure1=getStructuringElement (MORPH_RECT, Size (6, 6));
Dilate (bw, bw, Structure1, Point (1, 1));
Imshow (" display ", bw);
waitKey(0);

FindContours (bw, contours, hierarchy, RETR_EXTERNAL CHAIN_APPROX_SIMPLE);
if (! Contours. The empty () & amp; & ! Hierarchy. The empty ()) {
Independence idx=0;
n=0;
Vector Approx.
for (; Idx>=0; Independence idx=hierarchy [independence idx] [0]) {
Color=colors [15] independence idx %;
//drawContours (smallImg, contours, independence idx, color, and 1, 8, hierarchy);
ApproxPolyDP (Mat (contours [independence idx]), approx, arcLength (Mat (contours [independence idx]), true) * 0.005, true);//0.005 will flash straightening coefficient
Const Point * p=& amp; Approx [0].
Int m=(int) approx. The size ();
Polylines (smallImg, & amp; P, & amp; M, 1, true, color);
Circle (smallImg, Point (p [0]. X, p [0]. Y), 3, color);
Circle (smallImg, Point (p [1]. X, p [1]. Y), 2, color);
For (int I=2; in++;
If (1==n) {
Maxrect=boundingRect (Mat (contours [independence idx]));
} else {
Brect=boundingRect (Mat (contours [independence idx]));
CvRect Mr (maxrect), br (brect);
Maxrect=cvMaxRect (& amp; Mr, & amp; Br);
}
}
Circle (smallImg, Point (maxrect. X + maxrect. Width/2, maxrect. Y + maxrect. Height/2), 2, CV_RGB (0, 255));
}
Imshow (" display ", smallImg);
waitKey(0);
CvDestroyWindow (" display ");
return 0;
}

CodePudding user response:

Opencv Mat if not overloaded operator - () function, should not the problem of opencv, you use pure opencv

CodePudding user response:

Appear this kind of problem I think should be two pictures in turn gray image pixel gray value is not the same as the same part of the cause, you can do it to a poor image again binarization,
  • Related