gpt4 book ai didi

c++ - 使用之字形扫描将 8x8 矩阵转换为展平 vector

转载 作者:行者123 更新时间:2023-11-28 04:03:21 25 4
gpt4 key购买 nike

因此,我需要使用之字形扫描将 8x8 的 opencv 垫转换为展平 vector ,如图所示。

ZIG ZAG Explanation

我明白它应该做什么,我想我已经完成了前半部分的实现,但是当我尝试将值设置为 vector 时收到错误。

std::vector<float> *ZigZagScanner::scan(cv::Mat &input) {
std::vector<float> *output = new std::vector<float>();
// TODO Traverse the input in a zigzag scan, and store the result in output
//set row and column start values to zero, set increment flag to false

// TODO Traverse the input in a zigzag scan, and store the result in output
//set row and column start values to zero, set increment flag to false
int row, col = 0;
bool increment = false;

//create nest for loops to traverse through the first half of the matrix in a zig zag fashion
for(int y = 1; y <= 8; ++y){
for(int x = 0; x < y; ++x){
//add the current row and column to the flatten vector

//ERROR HERE
cv::Rect rect = cv::Rect(y,x, 8, 8);
output->push_back(new cv::Mat(input, rect));


if(x + 1 == y){
break;
}

//when the increment flag is true increase the row and decrease the column
if(increment == true){
++row, --col;
}
else{
--row, ++col;
}
}
//if y gets to out of bounds break the loop
if(y == 8){
break;
}
//if the increment flag is true then increment the row and switch the flag, otherwise increment the column and swap the flag
if(increment == true){
++row, increment = false;
}
else{
++col, increment = true;
}
}

//update the columns and rows to the correct values to go through the second half of the matrix
if(row == 0){
if(col == 7){
++row;
}
else{
++col;
increment = true;
}
}
else{
if(row == 7){
++col;
}
else{
++row;
increment = false;
}
}

for(int k, j = 7; j > 0; --j){
if(j > 8){
k = 8;
}
else{
k = j;
}

for(int i = 0; i < k; i++){

//ERROR HERE AS WELL
cv::Rect rect = cv::Rect(y,x, 8, 8);
output->push_back(new cv::Mat(input, rect));
}
}

在这一点上,我只是在努力弄清楚这部分,任何建议都意义重大! 返回输出;

最佳答案

您的输出 vector 存储 float , 那你为什么要尝试将指针推到 cv::Mat在那里?

如果你有 8x8 的浮点矩阵,只需使用 .at<float>(y,x)访问 input 的一个浮点值的方法矩阵。

output->push_back(input.at<float>(y-1,x)); // -1 because you iterate in range <1,8>

您的方法似乎您想使用 Rect作为 ROI 并将其应用于输入矩阵。如果你想获得输入的子区域 Mat作为 1x1 矩形,您可以:

cv::Rect roi(x,y-1,1,1); // 1x1 matrix
output->push_back( input(roi).at<float>(0,0) );

另外我不明白为什么你使用 N 循环而不是成对的数组来制作之字形顺序:

std::pair<int,int> zigZagOrder[64] = { {0,0},{1,0},{1,0},...};

然后只查找这个。

在图像处理中,每一毫秒都很重要,不要将时间浪费在花哨的锯齿形顺序上。

关于c++ - 使用之字形扫描将 8x8 矩阵转换为展平 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59150761/

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