我正在嘗試制作一個簡單的 Cuda 應用程式,它可以創建給定矩陣的積分影像。我需要做的步驟之一是創建每一行的完整影像。為了做到這一點,我想為每一行分配 1 個執行緒。應該執行此操作的函式:
__global__ void IntegrateRows(const uchar* img, uchar* res)
{
int x = blockIdx.x * blockDim.x threadIdx.x;
int y = blockIdx.y * blockDim.y threadIdx.y;
if (x >= Width || y >= Height)
return;
int sum = 0;
int row = y * Width;
for (int i = 0; i < Width - x; i)
{
res[row i x] = sum img[row i x];
sum = img[row i x];
}
}
我使用大小為 3840x2160 的矩陣填充了一個 ( cv::Mat::ones(Size(Width, Height), CV_8UC1)) 進行測驗。當我嘗試列印結果的內容時,它總是回傳從 1 到 255 的數字序列:

執行配置為:
dim3 threadsPerBlock(1, 256);
dim3 numBlocks(1, 16);
IntegrateRows<<<numBlocks, threadsPerBlock >>>(img, res);
我的 GPU 是 Nvidia RTX 3090。
uj5u.com熱心網友回復:
tl;dr:讓你的輸出矩陣有更大的元素
如果您對序列進行積分/前綴求和
1, 1, 1, 1, ...
你得到:
0, 1, 2, 3, ...
當您達到元素型別的最大值時,此序列將回傳到 0。在你的情況下,它是一個uchar,即unsigned char。它的最大值是 255。再加上 1,得到 0。所以:0, 1, 2, 3, ... 253, 254, 255, 0, 1, ... 等等。
如果您將輸出矩陣元素型別更改為unsigned short(或者可能只是unsigned int) - 您將不會獲得環繞行為。當然,如果你加起來是 255 而不是 1,和/或你的矩陣更大,那么型別的表示范圍可能不夠大。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/340890.html
上一篇:使用Python對影像中高于閾值的像素值求和的最快方法
下一篇:如何洗掉沒有檔案擴展名的檔案?
