c++ mat赋值_问一下c++赋值的有关问题,这个应该如何写

问一下c++赋值的问题,这个应该怎么写?

/*

Copyright (C) 2006 Pedro Felzenszwalb

This program is free software; you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation; either version 2 of the License, or

(at your option) any later version.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU General Public License for more details.

You should have received a copy of the GNU General Public License

along with this program; if not, write to the Free Software

Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

*/

/* a simple image class */

#ifndef IMAGE_H

#define IMAGE_H

#include 

template 

class image

{

public:

/* create an image */

image(const int width, const int height, const bool init = true);

/* delete an image */

~image();

/* init an image */

void init(const T &val);

/* copy an image */

image *copy() const;

/* get the width of an image. */

int width() const { return w; }

/* get the height of an image. */

int height() const { return h; }

/* image data. */

T *data;

/* row pointers. */

T **access;

public:

int w, h;

};

/* use imRef to access image data. */

#define imRef(im, x, y) (im->access[y][x])

/* use imPtr to get pointer to image data. */

#define imPtr(im, x, y) &(im->access[y][x])

template 

image::image(const int width, const int height, const bool init)

{

w = width;

h = height;

data = new T[w * h];  // allocate space for image data

access = new T*[h];   // allocate space for row pointers

// initialize row pointers

for (int i = 0; i 

access[i] = data + (i * w);

if (init)

memset(data, 0, w * h * sizeof(T));

}

template 

image::~image()

{

delete [] data;

delete [] access;

}

template 

void image::init(const T &val)

{

T *ptr = imPtr(this, 0, 0);

T *end = imPtr(this, w-1, h-1);

while (ptr <= end)

*ptr++ = val;

}

template 

image *image::copy() const

{

image *im = new image(w, h, false);

memcpy(im->data, data, w * h * sizeof(T));

return im;

}

#endif

int main(int argc, char **argv)

{

//if (argc != 6)

//{

//fprintf(stderr, "usage: %s sigma k min input(ppm) output(ppm)\n", argv[0]);

//return 1;

//}

//sigma 0.5 k 500 min 50

//参数说明http://cs.brown.edu/~pff/segment/

float sigma = 0;//atof(argv[1]);

float k = 0;//atof(argv[2]);

int min_size = 0;//atoi(argv[3]);

cin>>sigma;

cin>>k;

cin>>min_size;

printf("loading input image.\n");

char *image_input_name = "beach.ppm";

char *image_output_name ="output1.ppm";

image *input = loadPPM(image_input_name);

printf("processing\n");

int num_ccs;

image *seg = segment_image(input, sigma, k, min_size, &num_ccs);

savePPM(seg, image_output_name);

Mat seg_result(seg->w,seg->h,CV_8UC3);

for (int i = 0 ;i w;i++)

{

for (int j = 0 ; j h ;j++)

{

//uchar temp = 8<access[i][j];

seg_result.at(i,j)[0] = (char)seg->access[i][j]->b;

seg_result.at(i,j)[1] = (char)seg->access[i][j]->g;//y不知道怎赋值了

seg_result.at(i,j)[2] = (char)seg->access[i][j]->r;

}

}

printf("got %d components\n", num_ccs);

printf("done! uff...thats hard work.\n");

return 0;

}

Mat seg_result(seg->w,seg->h,CV_8UC3);

for (int i = 0 ;i w;i++)

{

for (int j = 0 ; j h ;j++)

{

//uchar temp = 8<access[i][j];

seg_result.at(i,j)[0] = (char)seg->access[i][j]->b;//这块应该怎么写?

seg_result.at(i,j)[1] = (char)seg->access[i][j]->g;

seg_result.at(i,j)[2] = (char)seg->access[i][j]->r;

}

}

1>e:\code\c++\graph_segmentation\main.cpp(82): error C2109: 下标要求数组或指针类型

1>e:\code\c++\graph_segmentation\main.cpp(82): error C2819: “rgb”类型没有重载成员“operator ->”

1>          e:\code\c++\graph_segmentation\misc.h(32) : 参见“rgb”的声明

1>          是否改用“.”?

1>e:\code\c++\graph_segmentation\main.cpp(82): error C2232: “->rgb::b”: 左操作数有“struct”类型,使用“.”

1>e:\code\c++\graph_segmentation\main.cpp(83): error C2109: 下标要求数组或指针类型

1>e:\code\c++\graph_segmentation\main.cpp(83): error C2819: “rgb”类型没有重载成员“operator ->”

1>          e:\code\c++\graph_segmentation\misc.h(32) : 参见“rgb”的声明

1>          是否改用“.”?

1>e:\code\c++\graph_segmentation\main.cpp(83): error C2232: “->rgb::g”: 左操作数有“struct”类型,使用“.”

1>e:\code\c++\graph_segmentation\main.cpp(84): error C2109: 下标要求数组或指针类型

1>e:\code\c++\graph_segmentation\main.cpp(84): error C2819: “rgb”类型没有重载成员“operator ->”

1>          e:\code\c++\graph_segmentation\misc.h(32) : 参见“rgb”的声明

1>          是否改用“.”?

1>e:\code\c++\graph_segmentation\main.cpp(84): error C2232: “->rgb::r”: 左操作数有“struct”类型,使用“.”

1>

1>生成失败。

1>

1>已用时间 00:00:00.64

------解决思路----------------------

(seg->access)[i][j]->b; 先加上括号

------解决思路----------------------

编译器说的很明白了呀

1>e:\code\c++\graph_segmentation\main.cpp(82): error C2232: “->rgb::b”: 左操作数有“struct”类型,使用“.”

-> 改成 . 呀

access 的类型 是 T**, 也就是 struct rgb **, 所以 access[i][j]的类型就是 struct rgb呀,引用成员用.啊

seg_result.at(i,j)[0] = (char)seg->access[i][j]->b;

改成

seg_result.at(i,j)[0] = (char)seg->access[i][j].b;

------解决思路----------------------

Mat seg_result(seg->w,seg->h,CV_8UC3); 是啥类型,

貌似提示的是等号左边的问题呀,

seg_result.at(i,j)[0] = (char)seg->access[i][j].b;

"下标要求数组或指针类型" 指的是等号左边的部分吧?

seg_result.at(i,j)[0]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值