gpt4 book ai didi

android - 如何在图像中选择正确的矩形?

转载 作者:太空狗 更新时间:2023-10-29 13:19:45 25 4
gpt4 key购买 nike

我想检测魔方的颜色。这就是我想要的:Link
我能够使用 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));
}
}

这是“原始”源图像:

Image

它产生这个输出(绿色圆圈:角;蓝色圆圈:矩形的中心):

Image

如您所见,检测到的矩形多于 9 个。我想获取点数组中的九个中点。
我怎样才能选择合适的?
希望你能明白我的意思

最佳答案

我已经在 OpenCV 中编写了执行此操作的代码。

基本过程与您的一样,找到轮廓,然后剔除小的和非凸的轮廓。

在此之后,您可以遍历轮廓,对每个轮廓执行以下操作:

  1. 使用 Y 然后 X 坐标按升序对边界像素进行排序
  2. 遍历这些点。对于每个 Y,将每个 X 和下一个 X 之间的所有点添加到向量中。您现在有一个向量中包含的所有点的向量。您还可以使用它来计算质心并计算平均 RGB 颜色,如下所示:

下面是一些示例代码,但请注意,它并不完整,但应该会给您一个大概的想法。

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);
}

}

关于android - 如何在图像中选择正确的矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30454208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com