- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我正在尝试编写一个将 RGB 图像转换为灰度图像的程序。我从 Udacity 问题集中得到了这个想法。问题是,当我在 Udacity 网络环境中写出内核时,它说我的代码可以工作,但是,当我尝试在我的计算机上本地执行时,我没有收到任何错误,但是我的图像不是灰度,而是完全变灰了。它看起来像我加载的图像尺寸的一个灰色框。你能帮我找到我代码中的错误吗,我已经将它与 Udacity 版本进行了比较,但我似乎找不到它。
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <string>
#include <cuda.h>
#include <stdio.h>
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <iostream>
#define CUDA_ERROR_CHECK
#define CudaSafeCall( err ) __cudaSafeCall( err, __FILE__, __LINE__ )
#define CudaCheckError() __cudaCheckError( __FILE__, __LINE__ )
inline void __cudaSafeCall(cudaError err, const char *file, const int line)
{
#ifdef CUDA_ERROR_CHECK
if (cudaSuccess != err)
{
fprintf(stderr, "cudaSafeCall() failed at %s:%i : %s\n",
file, line, cudaGetErrorString(err));
exit(-1);
}
#endif
return;
}
inline void __cudaCheckError(const char *file, const int line)
{
#ifdef CUDA_ERROR_CHECK
cudaError err = cudaGetLastError();
if (cudaSuccess != err)
{
fprintf(stderr, "cudaCheckError() failed at %s:%i : %s\n",
file, line, cudaGetErrorString(err));
exit(-1);
}
err = cudaDeviceSynchronize();
if (cudaSuccess != err)
{
fprintf(stderr, "cudaCheckError() with sync failed at %s:%i : %s\n",
file, line, cudaGetErrorString(err));
exit(-1);
}
#endif
return;
}
__global__ void rgb_2_grey(uchar* const greyImage, const uchar4* const rgbImage, int rows, int columns)
{
int rgb_x = blockIdx.x * blockDim.x + threadIdx.x; //x coordinate of pixel
int rgb_y = blockIdx.y * blockDim.y + threadIdx.y; //y coordinate of pixel
if ((rgb_x >= columns) && (rgb_y >= rows)) {
return;
}
int rgb_ab = rgb_y*columns + rgb_x; //absolute pixel position
uchar4 rgb_Img = rgbImage[rgb_ab];
greyImage[rgb_ab] = uchar((float(rgb_Img.x))*0.299f + (float(rgb_Img.y))*0.587f + (float(rgb_Img.z))*0.114f);
}
using namespace cv;
using namespace std;
void Proc_Img(uchar4** h_RGBImage, uchar** h_greyImage, uchar4 **d_RGBImage, uchar** d_greyImage);
void RGB_2_Greyscale(uchar* const d_greyImage, uchar4* const d_RGBImage, size_t num_Rows, size_t num_Cols);
void Save_Img();
Mat img_RGB;
Mat img_Grey;
uchar4 *d_rgbImg;
uchar *d_greyImg;
int main()
{
uchar4* h_rgbImg;
//uchar4* d_rgbImge=0;
uchar* h_greyImg;
//uchar* d_greyImge=0;
Proc_Img(&h_rgbImg, &h_greyImg, &d_rgbImg, &d_greyImg);
RGB_2_Greyscale(d_greyImg, d_rgbImg, img_RGB.rows, img_RGB.cols);
Save_Img();
return 0;
}
void Proc_Img(uchar4** h_RGBImage, uchar** h_greyImage, uchar4 **d_RGBImage, uchar** d_greyImage){
cudaFree(0);
CudaCheckError();
//loads image into a matrix object along with the colors in BGR format (must convert to rgb).
Mat img = imread("C:\\Users\\Austin\\Pictures\\wallpapers\\IMG_3581.JPG", CV_LOAD_IMAGE_COLOR);
if (img.empty()){
cerr << "couldnt open file dumbas..." << "C:\\Users\\Austin\\Pictures\\wallpapers\\IMG_3581.JPG" << endl;
exit(1);
}
//converts color type from BGR to RGB
cvtColor(img, img_RGB, CV_BGR2RGBA);
//allocate memory for new greyscale image.
//img.rows returns the range of pixels in y, img.cols returns range of pixels in x
//CV_8UC1 means 8 bit unsigned(non-negative) single channel of color, aka greyscale.
//all three of the parameters allow the create function in the Mat class to determine how much memory to allocate
img_Grey.create(img.rows, img.cols, CV_8UC1);
//creates rgb and greyscale image arrays
*h_RGBImage = (uchar4*)img_RGB.ptr<uchar>(0); //.ptr is a method in the mat class that returns a pointer to the first element of the matrix.
*h_greyImage = (uchar*)img_Grey.ptr<uchar>(0); //this is just like a regular array/pointer mem address to first element of the array. This is templated
//in this case the compiler runs the function for returning pointer of type unsigned char. for rgb image it is
//cast to uchar4 struct to hold r,g, and b values.
const size_t num_pix = (img_RGB.rows) * (img_RGB.cols); //amount of pixels
//allocate memory on gpu
cudaMalloc(d_RGBImage, sizeof(uchar4) * num_pix); //bites of 1 uchar4 times # of pixels gives number of bites necessary for array
CudaCheckError();
cudaMalloc(d_greyImage, sizeof(uchar) * num_pix);//bites of uchar times # pixels gives number of bites necessary for array
CudaCheckError();
cudaMemset(*d_greyImage, 0, sizeof(uchar) * num_pix);
CudaCheckError();
//copy array into allocated space
cudaMemcpy(*d_RGBImage, *h_RGBImage, sizeof(uchar4)*num_pix, cudaMemcpyHostToDevice);
CudaCheckError();
d_rgbImg = *d_RGBImage;
d_greyImg = *d_greyImage;
}
void RGB_2_Greyscale(uchar* const d_greyImage, uchar4* const d_RGBImage, size_t num_Rows, size_t num_Cols){
const int BS = 16;
const dim3 blockSize(BS, BS);
const dim3 gridSize((num_Cols / BS) + 1, (num_Rows / BS) + 1);
rgb_2_grey <<<gridSize, blockSize>>>(d_greyImage, d_RGBImage, num_Rows, num_Cols);
cudaDeviceSynchronize(); CudaCheckError();
}
void Save_Img(){
const size_t num_pix = (img_RGB.rows) * (img_RGB.cols);
cudaMemcpy(img_Grey.ptr<uchar>(0), d_greyImg, sizeof(uchar)*num_pix, cudaMemcpyDeviceToHost);
CudaCheckError();
imwrite("C:\\Users\\Austin\\Pictures\\wallpapers\\IMG_3581GR.JPG", img_Grey);
cudaFree(d_rgbImg);
cudaFree(d_greyImg);
}
编辑:我意识到我的 main 中的本地变量与全局变量同名,我已经在这里编辑了代码,现在我从 visual studio 中得到了错误
variable
d_rgbIme
is being used without being initialized
当我已经在上面初始化了它。如果我将它们设置为零,我会收到一个 CUDA 错误
an illegal memory access was encountered
我尝试运行 cuda-memcheck,但随后出现无法运行文件的错误...
最佳答案
感谢 Robert Crovella 的评论之一,我发现了错误,他对此非常有帮助!在我的内核中,if 语句应该是 if ((rgb_x >= columns) || (rgb_y >= rows)) {
关于c++ - 使用 CUDA 将 RGB 转换为灰度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25300433/
我拦截了一个数据包并提取了有效载荷。此有效负载是压缩的 jpeg 字节流数据(例如,将此数据分配给 unsigned char *payload )。我知道如果我有一个 FILE 指针,那么我可以使用
假设我们有一个单 channel 图像 (5x5) A = [ 1 2 3 4 5 6 7 8 9 2 1 4 5 6 3 4 5 6 7 4 3 4
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 2 年前。 Improve th
我有一个 RGB LED 并且有 9、10、11 的引脚和接地的引脚。已为 R、G 和 B 提供电阻器。 当我这样做时: analogWrite(r, 255); // I see a red c
我想知道如何从像素中获取颜色作为 RGB 整数,并在需要时进行转换。另外,如何利用差异来确定一个像素是否比另一个像素更亮或更暗。 最佳答案 简单: rgb_int = rgb_tuple[0] <<
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 3 年前。 Improve
我的目标是找到与数组中的 RGB 相比最接近的 RGB 匹配项。我已经创建了一个循环遍历图片中每个像素的函数。我现在唯一需要做的就是找到图片中每个像素最接近数组颜色的颜色。 $colors = arr
将 YUV 文件转换为 RGB 文件时出现问题。完成后,我无法使用 GIMP 或其他查看器打开 .rgb 文件,但我成功打开下载的 .rgb 文件。请告诉我RGB文件的结构是什么? (它是否包含标题?
我正在开发一个程序,在该程序中我获取图像的一部分并计算该图像的平均 RGB。当我计算它时,我得到完全不同的值,就好像我要使用内置函数一样。当我测试我的数字并将它们放入 RGB 颜色图表时,它们会关闭,
我正在尝试无损压缩图像,为了利用规律性,我想将图像从 RGB 转换为 Y'CbCr。 (我所说的 RGB 和 Y'CbCr 的具体细节在这里并不重要;RGB 数据由三个字节组成,我有三个字节来存储结果
我有一个应用程序可以生成一堆 jpg,我需要将这些 jpg 转换为 webm 视频。我正在尝试将 jpeg 中的 rgb 数据放入 vpxenc 示例中。我可以在输出视频中看到原始 jpg 的基本形状
我不太熟悉位移位,所以我有以下问题。我使用下面的函数(在别处找到)从 YUV 解码为 RGB int 数组。 现在我想调整红色或绿色或蓝色值来创建一些自定义滤镜效果。我需要检索 R 值、G 值和 B
在下面的代码片段中,我试图在 Visual C++ 中检索像素的 RGB 值,然后将相同的 RGB 值设置回相同的像素。也就是说,这只是一个测试。但是,当我这样做时,生成的图像相似但颜色错误/关闭。生
我试图在 上将 RGB 颜色从蓝色 (rgba(0,0,255)) 转换为红色 (rgba(255,0,0)) >JS mouseenter,渐进式。 因此,每次鼠标进入一个元素时,它都会“增加”其背
我需要根据像素的 RGB 颜色创建一个 Color 对象(读取 PNG 文件的 BufferedImage 对象,BufferedImage 颜色空间为 BufferedImage.TYPE_4BYT
我正在编写一段代码,它必须从 RGB 图像转换为 rgb 标准化空间。我已经使用 for 格式使用它,但它运行速度太慢,我需要评估大量图像。我正在尝试矢量化完整功能以加快它的速度。我现在有以下内容:
我想在多种光照条件下获取图像的 RGB 值。为了获得某种中性场景,我想使用一些预定义图像的 RGB 值对 RGB 值进行归一化。 让我解释一下。我有 6 张预定义图像,我知道它们的确切平均 RGB 值
将平行四边形((RGB)点的二维数组)投影到三角形((RGB)点的二维数组)(在我的特定情况下,将矩形投影到具有相同边长的直角三角形)的伪代码算法是什么(等腰),在我的例子中,斜边的大小与矩形的最大边
假设我有一张摄影底片扫描为 RGB 图像,我正试图找到一种算法将颜色值转换为 RGB 正片。 由于橙色偏差 (http://photo.net/learn/orange-negative-mask),
我已成功将图像转换为灰度图像,我想将灰度图像恢复为 RGB 图像。请帮忙。提前致谢。 -(UIImage *) toGrayscale { const int RED = 1;
我是一名优秀的程序员,十分优秀!