Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:52:49+00:00 2026-06-11T16:52:49+00:00

i began to implement some simple image processing using cuda but i have an

  • 0

i began to implement some simple image processing using cuda but i have an error in my code
the error happens when i copy pixels from device to host

this is my try

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <stdio.h> 
using namespace cv;

unsigned char *h_pixels;
unsigned char *d_pixels;
int bufferSize;
int width,height;

const int BLOCK_SIZE = 32;
Mat image;

void get_pixels(const char* fileName)
{
    image = imread(fileName);
    bufferSize = image.size().width * image.size().height * 3 * sizeof(unsigned char);
    width = image.size().width;
    height = image.size().height;
    h_pixels = new unsigned char[bufferSize];
    memcpy(h_pixels,image.data,bufferSize);
}

__global__ void invert_image(unsigned char* pixels,int width,int height)
{
    int row = blockIdx.y * BLOCK_SIZE + threadIdx.y;
    int col = blockIdx.x * BLOCK_SIZE + threadIdx.x;
    int cidx = (row  * width + col) * 3;
    pixels[cidx] = 255 - pixels[cidx]; 
    pixels[cidx + 1] = 255 - pixels[cidx + 1]; 
    pixels[cidx + 2] = 255 - pixels[cidx + 2]; 

}
int main()
{
    get_pixels("D:\\photos\\z.jpg");

    cudaError_t err = cudaMalloc((void**)&d_pixels,bufferSize);
    err =  cudaMemcpy(d_pixels,h_pixels,bufferSize,cudaMemcpyHostToDevice);
    dim3 dimBlock(BLOCK_SIZE,BLOCK_SIZE);
    dim3 dimGrid(width/dimBlock.x,height/dimBlock.y);

    invert_image<<<dimBlock,dimGrid>>>(d_pixels,width,height);

    unsigned char *pixels = new unsigned char[bufferSize];


    err= cudaMemcpy(pixels,d_pixels,bufferSize,cudaMemcpyDeviceToHost);// unknown error 
    const char * errStr = cudaGetErrorString(err);
    cudaFree(d_pixels);
    image.data = pixels;
    namedWindow("display image");
    imshow("display image",image);
    waitKey();
    return 0;
}

also how can i find out error that occurs in cuda device
thanks for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:52:51+00:00Added an answer on June 11, 2026 at 4:52 pm
    • First of all be sure that the image file is read correctly.
    • Check if the device memory is allocated with CUDA_SAFE_CALL(cudaMalloc(..))
    • Check the dimensions of the image. If the dimension of the image is not multiples of BLOCKSIZE than you might be missing some indices and the image is not fully inverted.
    • Call cudaDeviceSynchronize after the kernel call and check its return value.
    • Do you get any error when you run the code without calling the kernel anyway?
    • You are not freeing the h_pixels and might have a memory leak.
    • Instead of using BLOCKSIZE in the kernel you might use “blockDim.x”. So calculating indices like “blockIdx.x * blockDim.x + threadIdx.x”
    • Try to do not touch the memory area in the kernel code, namely comment out the memory updates at the kernel (the lines where you access the pixels array) and check if the program continues to fail. If it does not continue to fail you might be accessing out of the bounds.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently began using C# to invoke powershell scripts, with some success :)
I began reading the book Code Complete 2nd edition, but stopped reading when I
I began writing an app using declarative_authorization ( http://github.com/stffn/declarative_authorization ) but I'm now wondering
I've got some long but simple loops in my Delphi program that may loop
I've recently begun using MathNet to implement our linear algebra, however I'm having some
I am trying to implement a simple DAO using hibernate 4 and Spring 3.
I began studying Java Regular Expression recently and I found some really intersting task.For
Only began using Haskell a couple of weeks ago - I am attempting to
Ok, stuck on another Python Question. I have tried this a few times, but
Okay, thus may seen kind of odd, but I wanted to get some suggestions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.