我正在尝试计算下面骨架图像中的终点数。我在这里使用矢量。我需要的是打印矢量,而不仅仅是计数。我试过很多方面。但没有奏效。我是新手打开简历,我从互联网上找到了以下代码。任何人都可以帮我打印这段代码实际得到的向量。代码如下。
// get the end points
// Declare variable to count neighbourhood pixels
int count, numberOFEndpoints;
// To store a pixel intensity
uchar pix;
numberOFEndpoints = 0;
// To store the ending co-ordinates
std::vector<int> coords;
// For each pixel in our image...
for (int i = 1; i < CopyofSkeletionize.rows - 1; i++) {
for (int j = 1; j < CopyofSkeletionize.cols - 1; j++) {
// See what the pixel is at this location
pix = CopyofSkeletionize.at<uchar>(i, j);
// If not a skeleton point, skip
if (pix == 0)
continue;
// Reset counter
count = 0;
// For each pixel in the neighbourhood
// centered at this skeleton location...
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
// Get the pixel in the neighbourhood
pix = CopyofSkeletionize.at<uchar>(i + y, j + x);
// Count if non-zero
if (pix != 0)
count++;
}
}
// If count is exactly 2, add co-ordinates to vector
if (count == 2) {
coords.push_back(i);
coords.push_back(j);
numberOFEndpoints = numberOFEndpoints + 1;
}
}
}
printf("numberOFEndpoints : %d \n", numberOFEndpoints);
我在互联网上使用了这段代码,但它没有用。 for(int i = 0; i&lt; coords.size()/ 2; i ++) cout&lt;&lt; “(”&lt;
答案 0 :(得分:0)
首先......你想以更好的方式存储坐标。也许像两个载体:
std::vector<int> coordx;
std::vector<int> coordy;
然后你打印它:
for (int i = 0; i < coordx.size(); ++i)
std::cout << coordx[i] << " x " << coordy[i] << std::endl;
或者使用pair
(可能需要#include <pair>
)或创建自定义point
结构:
std::vector< std::pair<int, int> > coords;
添加到coords矢量会是这样的:
coords.push_back(std::make_pair(x, y));
然后你打印它:
for (int i = 0; i < coords.size(); ++i)
std::cout << coords[i].first << " x " << coords[i].second << std::endl;
其次......如果您坚持使用单vector<int>
,请尝试:
int i = 0;
while (i < coord.size())
std::cout << coords[i++] << " x " << coords[i++] << std::endl;
答案 1 :(得分:0)
// get the end points
// Declare variable to count neighbourhood pixels
int count, numberOFEndpoints;
// To store a pixel intensity
uchar pix;
numberOFEndpoints = 0;
// To store the ending co-ordinates
/*std::vector<int> coords;*/
std::vector<int> coordx;
std::vector<int> coordy;
// For each pixel in our image...
for (int i = 1; i < CopyofSkeletionize.rows - 1; i++) {
for (int j = 1; j < CopyofSkeletionize.cols - 1; j++) {
// See what the pixel is at this location
pix = CopyofSkeletionize.at<uchar>(i, j);
// If not a skeleton point, skip
if (pix == 0)
continue;
// Reset counter
count = 0;
// For each pixel in the neighbourhood
// centered at this skeleton location...
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
// Get the pixel in the neighbourhood
pix = CopyofSkeletionize.at<uchar>(i + y, j + x);
// Count if non-zero
if (pix != 0)
count++;
}
}
// If count is exactly 2, add co-ordinates to vector
if (count == 2) {
coordx.push_back(i);
coordy.push_back(j);
/*numberOFEndpoints = numberOFEndpoints + 1;*/
}
}
}
for (int i = 0; i < coordx.size(); ++i)
std::cout << coordx[i] << " x " << coordy[i] << std::endl;}
我甚至无法从此代码中获取矢量结果