图像为什么只被部分处理?

huangapple go评论87阅读模式
英文:

Why is the image being partially processed?

问题

我已经找到问题所在。是我读取图像的方式有问题。

应该改成:

  1. img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)

现在它可以正常工作了,尽管由于某种原因,它的运行时间是我另一个执行相同操作的脚本的10倍... 嗯...

英文:

It is been hours writing scripts and I think I am tired overlooking something simple.
I have the following pycuda script

  1. import cv2
  2. import numpy as np
  3. import time
  4. import pycuda.autoinit
  5. import pycuda.driver as cuda
  6. from pycuda.compiler import SourceModule
  7. import pycuda.gpuarray as gpuarray
  8. def apply_threshold(img_src,img_width, img_height, img_dest, mythreshold):
  9. mod = SourceModule("""
  10. __global__ void ThresholdKernel(
  11. const int src_sizeX, //< source image size. x: width,
  12. const unsigned char* src, //< source image pointer
  13. const int dst_sizeX, //< destination image size. x: width, y: height
  14. const int dst_sizeY,
  15. unsigned char* dst, //< destination image pointer
  16. const int mythreshold) {
  17. int col = blockIdx.x * blockDim.x + threadIdx.x;
  18. int row = blockIdx.y * blockDim.y + threadIdx.y;
  19. if (dst_sizeX <= col || dst_sizeY <= row) return;
  20. auto src_val = src[row * src_sizeX + col];
  21. unsigned char dst_val = src_val > mythreshold ? 255 : 0;
  22. dst[row * dst_sizeX + col] = dst_val;
  23. }
  24. """)
  25. block_dim =(32,8,1)
  26. grid_dim_x = (img_width + block_dim[0] -1) // block_dim[0]
  27. grid_dim_y = (img_width + block_dim[1] -1) // block_dim[1]
  28. print(grid_dim_x,grid_dim_y)
  29. thresholdkernel = mod.get_function("ThresholdKernel")
  30. thresholdkernel(np.int32(img_width), img_src, np.int32(img_width),np.int32(img_height),
  31. img_dest,np.int32(mythreshold),
  32. block = block_dim , grid = (grid_dim_x,grid_dim_y))
  33. mythreshold = 128
  34. img_path = "../images/lena_gray.png"
  35. img = cv2.imread(img_path)
  36. if img is None:
  37. print("Image not found")
  38. exit()
  39. else:
  40. height,width,channels = img.shape
  41. print("Hegiht, width and channels",height,width,channels)
  42. print(type(width))
  43. img_gpu = cuda.mem_alloc(img.nbytes)
  44. cuda.memcpy_htod(img_gpu,img)
  45. dtype=img.dtype
  46. # dest_img=gpuarray.empty_like(img.shape,dtype=dtype)
  47. dest_img = cuda.mem_alloc(img.nbytes)
  48. apply_threshold(img_gpu,width,height,dest_img ,mythreshold )
  49. image_result= np.empty_like(img)
  50. cuda.memcpy_dtoh(image_result,dest_img )
  51. cv2.imshow("Original image",img)
  52. cv2.imshow("Thresholded",image_result)
  53. cv2.waitKey(0)
  54. cv2.destroyAllWindows()

When I run it I get a binarized picture but this one

图像为什么只被部分处理?

What am I overlooking that makes the kernel only process part of the image? It must be something really simple

EDIT: I found the problem. The way I am reading the image.

It should be

  1. img = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)

Now it works, although for some reason it takes 10 times the time of a similar script I have that does the same... well...

答案1

得分: 1

我猜测这是因为你在grid_dim_xgrid_dim_y都使用了img_width。但你可能想要将img_height用于grid_dim_y

试试这个:

  1. grid_dim_x = (img_width + block_dim[0] -1) // block_dim[0]
  2. grid_dim_y = (img_height + block_dim[1] -1) // block_dim[1]
英文:

I assume it's because you are using img_width for both grid_dim_x and grid_dim_y. But you probably meant to use img_height for grid_dim_y.

Give this a shot:

  1. grid_dim_x = (img_width + block_dim[0] -1) // block_dim[0]
  2. grid_dim_y = (img_height + block_dim[1] -1) // block_dim[1]

huangapple
  • 本文由 发表于 2023年7月23日 13:31:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定