Android 如何选择图像中的右矩形?
我想检测Rubiks立方体的颜色。这就是我想要的:Android 如何选择图像中的右矩形?,android,opencv,image-processing,opencv3.0,opencv4android,Android,Opencv,Image Processing,Opencv3.0,Opencv4android,我想检测Rubiks立方体的颜色。这就是我想要的: 我能够通过Open CV的findContours功能识别9个彩色字段。 这是我的密码: Mat input = new Mat(); //The image Mat blur = new Mat(); Mat canny = new Mat(); Imgproc.GaussianBlur(input, blur, new Size(3,3), 1.5); //GaussianBlur to reduce noise Imgproc.Can
我能够通过Open CV的
findContours
功能识别9个彩色字段。这是我的密码:
Mat input = new Mat(); //The image
Mat blur = new Mat();
Mat canny = new Mat();
Imgproc.GaussianBlur(input, blur, new Size(3,3), 1.5); //GaussianBlur to reduce noise
Imgproc.Canny(blur, canny, 60, 70); //Canny to detect the edges
Imgproc.GaussianBlur(canny, canny, new Size(3,3), 1.5); //Again GaussianBlur to reduce noise
List<MatOfPoint> contours = new ArrayList<>();
Mat hierachy = new Mat();
Imgproc.findContours(canny, contours, hierachy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); //Find contours
List<MatOfPoint2f> approxedShapes = new ArrayList<>();
for(MatOfPoint point : contours){
double area = Imgproc.contourArea(point);
if(area > 1000){
MatOfPoint2f shape = new MatOfPoint2f(point.toArray());
MatOfPoint2f approxedShape = new MatOfPoint2f();
double epsilon = Imgproc.arcLength(shape, true) / 10;
Imgproc.approxPolyDP(shape, approxedShape, epsilon, true); //"Smooth" the edges with approxPolyDP
approxedShapes.add(approxedShape);
}
}
//Visualisation
for(MatOfPoint2f point : approxedShapes){
RotatedRect rect = Imgproc.minAreaRect(new MatOfPoint2f(point.toArray()));
Imgproc.circle(input, rect.center, 5, new Scalar(0, 0, 255));
for(Point p : point.toArray()){
Imgproc.circle(input, p, 5, new Scalar(0,255,0));
}
}
Mat输入=新Mat()//形象
Mat blur=新Mat();
Mat canny=新Mat();
高斯模糊(输入,模糊,新大小(3,3),1.5)//高斯消噪
Imgproc.Canny(模糊,Canny,60,70)//敏锐地检测边缘
GaussianBlur(canny,canny,新尺寸(3,3),1.5)//再次使用高斯模糊来降低噪声
列表等高线=新的ArrayList();
Mat层次=新Mat();
Imgproc.findContours(canny、等高线、层次结构、Imgproc.RETR\u列表、Imgproc.CHAIN\u近似简单)//寻找轮廓
List approxedShapes=new ArrayList();
对于(点:等高线){
双面积=Imgproc轮廓面积(点);
如果(面积>1000){
MatOfPoint2f形状=新的MatOfPoint2f(point.toArray());
MatOfPoint2f approxedShape=新的MatOfPoint2f();
双ε=Imgproc.弧长(形状,真)/10;
Imgproc.approxPolyDP(shape,approxedShape,epsilon,true);//用approxPolyDP“平滑”边缘
approxedShapes.add(approxedShapes);
}
}
//想象
对于(MatOfPoint2f点:近似形状){
RotatedRect rect=Imgproc.minareact(新的MatOfPoint2f(point.toArray());
Imgproc.circle(输入,rect.center,5,新标量(0,0,255));
对于(点p:Point.toArray()){
Imgproc.圆(输入,p,5,新标量(0255,0));
}
}
这是“原始”源图像:
它生成此输出(绿色圆圈:角;蓝色圆圈:矩形的中心):
如您所见,检测到的矩形多于9个。我想得到点数组中的九个中点。我怎样才能选择正确的呢?
希望您能理解我的意思我已经在OpenCV中编写了这样做的代码 基本过程和你的一样,找到轮廓,然后剔除小的非凸轮廓 在此之后,您可以在轮廓上迭代,对每个轮廓执行以下操作:
void meanColourOfContour( const Mat& frame, vector<Point> contour, Vec3b& colour, vector<Point>& pointsInContour ) {
sort(contour.begin(), contour.end(), pointSorter);
//
// Mean RGB values
//
int rsum = 0;
int gsum = 0;
int bsum = 0;
int index = 0;
Point lastP = contour[index++];
pointsInContour.push_back(lastP);
Vec3b rgbValue = frame.at<Vec3b>(lastP);
rsum += rgbValue[0];
gsum += rgbValue[1];
bsum += rgbValue[2];
int currentRow = lastP.y;
int lastX = lastP.x;
// For all remaining points in contour
while( index < contour.size() ) {
Point nextP = contour[index];
// Save it
pointsInContour.push_back(nextP);
// If we're on the same row, add in values of intervening points
if( nextP.y == currentRow ) {
for( int x = lastX; x < nextP.x; x++ ) {
Point p(x, currentRow);
pointsInContour.push_back(p);
rgbValue = frame.at<Vec3b>(p);
rsum += rgbValue[0];
gsum += rgbValue[1];
bsum += rgbValue[2];
}
}
// Add nextP
rgbValue = frame.at<Vec3b>(nextP);
rsum += rgbValue[0];
gsum += rgbValue[1];
bsum += rgbValue[2];
lastX = nextP.x;
currentRow = nextP.y;
index++;
}
// Calculate mean
size_t pointCount = pointsInContour.size();
colour =Vec3b( rsum/pointCount, gsum/pointCount, bsum/pointCount);
}
void extractFacelets( const Mat& frame, vector<tFacelet>& facelets) {
// Convert to Grey
Mat greyFrame;
cvtColor(frame, greyFrame, CV_BGR2GRAY);
blur( greyFrame, greyFrame, Size(3,3));
// Canny and find contours
Mat cannyOut;
Canny(greyFrame, cannyOut, 100, 200);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(cannyOut, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
// Filter out non convex contours
for( int i=contours.size()-1; i>=0; i-- ) {
if( contourArea(contours[i]) < 400 ) {
contours.erase(contours.begin()+i);
}
}
// For each contour, calculate mean RGB and plot in output
int cindex = 0;
for( auto iter = contours.begin(); iter != contours.end(); iter ++ ) {
// Sort points in contour on ascending Y then X coord
vector<Point> contour = (vector<Point>)*iter;
vector<Vec3b> meanColours;
Vec3b meanColour;
vector<Point> pointsInContour;
meanColourOfContour(frame, contour, meanColour, pointsInContour);
meanColours.push_back(meanColour);
long x=0; long y=0;
for( auto iter=pointsInContour.begin(); iter != pointsInContour.end(); iter++ ) {
Point p = (Point) *iter;
x += p.x;
y += p.y;
}
tFacelet f;
f.centroid.x = (int) (x / pointsInContour.size());
f.centroid.y = (int) (y / pointsInContour.size());
f.colour = meanColour;
f.visible = true;
facelets.push_back(f);
}
}
void表示轮廓的颜色(常数矩阵和帧、向量轮廓、向量3b和颜色、向量和点轮廓){
排序(contour.begin()、contour.end()、pointSorter);
//
//平均RGB值
//
int rsum=0;
int-gsum=0;
int-bsum=0;
int指数=0;
点lastP=等高线[index++];
点等高线。推回(最后一次);
Vec3b rgbValue=帧在(lastP);
rsum+=rgbValue[0];
gsum+=rgbValue[1];
bsum+=rgbValue[2];
int currentRow=lastP.y;
int lastX=lastP.x;
//对于轮廓中的所有剩余点
而(索引=0;i--){
if(轮廓面积(轮廓[i])<400){
轮廓。擦除(轮廓。开始()+i);
}
}
//对于每个轮廓,计算平均RGB并在输出中绘制
int-cindex=0;
对于(自动iter=courts.begin();iter!=courts.end();iter++){
//对等高线中的点按升序Y然后X坐标排序
矢量轮廓=(矢量)*iter;
矢量平均颜色;
矢量彩色;
矢量点轮廓;
轮廓的平均颜色(框架、轮廓、平均颜色、点轮廓);
meancolors.向后推(meancolor);
长x=0;长y=0;
对于(自动iter=pointsInContour.begin();iter!=pointsInContour.end();iter++){
点p=(点)*iter;
x+=p.x;
y+=p.y;
}
tFacelet f;
f、 centroid.x=(int)(x/pointsInContour.size());
f、 形心.y=(int)(y/pointsInContour.size());
f、 颜色=平均颜色;
f、 可见=真实;
小脸。推回(f);
}
}
您是否可以在不进行任何后期处理的情况下上传您的输入图像?这样我们就可以尝试我们的方法了?