C++ 在OpenCL中读取像素RGB值时出现问题
我需要从图像的两个部分(宽度和高度相同)读取像素(例如正方形([0,0],[300,300])和([400,0],[700300]),并对每个像素进行差异化 这是C(伪)代码:C++ 在OpenCL中读取像素RGB值时出现问题,c++,c,image,image-processing,opencl,C++,C,Image,Image Processing,Opencl,我需要从图像的两个部分(宽度和高度相同)读取像素(例如正方形([0,0],[300,300])和([400,0],[700300]),并对每个像素进行差异化 这是C(伪)代码: /** * @param img Input image * @param pos Integer position of top left corner of the second square (in this case 400) */ double getSum(Image& img, int pos
/**
* @param img Input image
* @param pos Integer position of top left corner of the second square (in this case 400)
*/
double getSum(Image& img, int pos)
{
const int width_of_cut = 300;
int right_bottom = pos + width;
Rgb first, second;
double ret_val = 0.0;
for(int i=0; i < width_of_cut; i++)
{
for(int j=0; j < width_of_cut; j++)
{
first = img.getPixel( i, j );
second = img.getPixel( i + pos, j );
ret_val += ( first.R - second.R ) +
( first.G - second.G ) +
( first.B - second.B );
}
}
return ret_val;
}
我是OpenCL的新手,我不知道我做错了什么
更新(1d图像):
我更改了内核代码。现在我在一个循环中读取一维图像,但仍然没有得到正确的值。我不确定我是否知道如何正确读取1d图像中的像素
__kernel void getSum( __read_only image1d_t input,
const int x_coord,
__global float* output,
const int img_width )
{
const int width_of_cut = 300;
int i = (int)(get_global_id(0));
for(int j=0; j < width_of_cut; j++)
{
int f = ( img_width*i + j );
int s = f + x_coord;
float4 first = read_imagef( input, sampler, f ); //pixel from 1st sq.
float4 second = read_imagef( input, sampler, s ); //pixel from 2nd sq.
output[get_global_id(0)] += ((first.x - second.x) +
(first.y - second.y) +
(first.z - second.z));
}
}
\uuuuuu内核void getSum(\uuuuu只读图像1d\t输入,
康斯特国际公司,
__全球浮动*输出,
常数(单位宽度)
{
const int width_of_cut=300;
int i=(int)(获取全局id(0));
对于(int j=0;j<切割的宽度;j++)
{
int f=(img_宽度*i+j);
int s=f+x_坐标;
float4 first=read_imagef(输入,采样器,f);//从第一个sq开始的像素。
float4 second=读取图像f(输入,采样器,s);//从第二个sq开始的像素。
输出[get_global_id(0)]+=((first.x-second.x)+
(first.y-second.y)+
(first.z-second.z));
}
}
竞争条件
所有垂直工作项都在访问相同的输出内存(output[get\u global\u id(0)]+=
),而不是原子地访问。因此,结果可能不正确(例如,两个线程读取相同的值,向其中添加一些内容,然后将其写回。只有一个线程获胜)
如果您的设备支持它,您可以将其设置为原子操作,但速度会很慢。您最好运行一个1D内核,该内核有一个垂直累积这些循环(因此,C示例中的j
循环)。竞争条件
所有垂直工作项都在访问相同的输出内存(output[get\u global\u id(0)]+=
),而不是原子地访问。因此,结果可能不正确(例如,两个线程读取相同的值,向其中添加一些内容,然后将其写回。只有一个线程获胜)
如果您的设备支持它,您可以将其设置为原子操作,但速度会很慢。您最好运行一个1D内核,该内核有一个垂直累积这些循环(因此,C示例中的
j
循环)。非常感谢您的回答。我更新了我的帖子-我试图按照你的建议更改我的内核代码,但我不确定我是否正确读取(索引)图像像素。你能告诉我错误在哪里吗?看起来不错,但为什么要使用1D图像?看起来您的原始代码是2D。因此,我认为您应该在内核中使用2D图像。非常感谢您的回答。我更新了我的帖子-我试图按照你的建议更改我的内核代码,但我不确定我是否正确读取(索引)图像像素。你能告诉我错误在哪里吗?看起来不错,但为什么要使用1D图像?看起来您的原始代码是2D。因此,我认为您应该在内核中使用2D图像。
__kernel void getSum( __read_only image1d_t input,
const int x_coord,
__global float* output,
const int img_width )
{
const int width_of_cut = 300;
int i = (int)(get_global_id(0));
for(int j=0; j < width_of_cut; j++)
{
int f = ( img_width*i + j );
int s = f + x_coord;
float4 first = read_imagef( input, sampler, f ); //pixel from 1st sq.
float4 second = read_imagef( input, sampler, s ); //pixel from 2nd sq.
output[get_global_id(0)] += ((first.x - second.x) +
(first.y - second.y) +
(first.z - second.z));
}
}