Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将图像转换为占用栅格_C++_Opencv - Fatal编程技术网

C++ 将图像转换为占用栅格

C++ 将图像转换为占用栅格,c++,opencv,C++,Opencv,我刚刚开始学习OpenCV,我想知道如何转换像这样的图像: int grid[ROW][COL] = { { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, { 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 }, { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 }, { 0, 0, 1, 0, 1, 0, 0, 0, 0, 1 }, { 1, 1, 1,

我刚刚开始学习OpenCV,我想知道如何转换像这样的图像:

int grid[ROW][COL] = 
    { 
        { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, 
        { 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 }, 
        { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 }, 
        { 0, 0, 1, 0, 1, 0, 0, 0, 0, 1 }, 
        { 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 }, 
        { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }, 
        { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 }, 
        { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, 
        { 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 } 
    }; 

1: cell is not blocked (white pixel).
0: cell is blocked (black pixel).

进入占用率网格,如下图所示:

int grid[ROW][COL] = 
    { 
        { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, 
        { 1, 1, 1, 0, 1, 1, 1, 0, 1, 1 }, 
        { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 }, 
        { 0, 0, 1, 0, 1, 0, 0, 0, 0, 1 }, 
        { 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 }, 
        { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }, 
        { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 }, 
        { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 }, 
        { 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 } 
    }; 

1: cell is not blocked (white pixel).
0: cell is blocked (black pixel).
我不会完全用那张照片。我打算用一幅只有墙壁的画:没有文字,没有家具,没有窗户,也没有门的符号。只有带有“孔”的墙才能显示门

我想读取图像,当像素为白色时返回1,当像素为黑色时返回0。只有那个

如何使用OpenCV实现这一点

我将把这个矩阵存储到一个文本文件中,但我知道怎么做


别担心我会怎么处理那个矩阵。我不是这样问的。

Mat
在OpenCV中,就像你提到的网格一样
.pgm
是机器人操作系统中用于存储占用栅格地图的格式。任何图像格式都适合表示

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{   
   //read input image as gray
   Mat image_gray = imread("input.jpg", CV_LOAD_IMAGE_GRAYSCALE);   

   // convert gray image to binary image 
   // After threshold, all values are either (0 or 200)
   Mat imgage_bw;
   cv::threshold(image_gray, imgage_bw, 200, 255.0, THRESH_BINARY);

   // if you really want images with 0 for blocked cell and 1 for free cell
    Mat image_grid = imgage_bw/255;  

    // save to disk
    imwrite("output.pgm", image_grid);

//write result to text file
FileStorage file("ouput.yaml", cv::FileStorage::WRITE);    
file <<"grid " <<image_grid;
file.release(); 

    return 0;
}

所提供的图像和网格是否相关?您必须指定如何从图像中获取网格中的
0
1
值,然后才能开始考虑解决实际(编程?)问题的方法。首先是将图像作为网格采样,那个么一个基本的实现就是,若方格完全是白色的,那个么它并没有被阻挡。我不理解这个问题。如果图像仅包含黑白像素,问题出在哪里?阅读图像,其中已经有零值和非零值。将非零转换为一,您就完成了。@user1241小错误在哪里?在这个问题上还是在下面的回答中?