(Xor,Yor)會歸屬於某一塊。分塊時,每一塊的初始地址(左上角坐標)可以確定。
便捷起見,先確定出塊的序號,分兩個維度統計。
塊序號:
block_numx=ceil(Xor+1)/16
block_numy=ceil(yOr+1)/16
每塊的初始地址為:16(n-1)。邊長可以隨便定,將16換就可以了。
代碼不對:
int xo=0,yo=0; for (int y=yo;y<ceil(dbZoom*iHeight);y++) { for (int x=xo;x<ceil(dbZoom*iWidth);x++) {int newWidth=ceil(dbZoom*iWidth); for(int i=1;i<=floor((iHeight+0.1)/16)-1;i++) { if((xOr-1>=0+16*(i-1) && xOr<=16*i && yOr-1>=0+16*(i-1) && yOr<=16*i)) { pbTag[y*newWidth+x]=255-20*i; } } }
}
代碼好像對:
int newWidth=ceil(dbZoom*iWidth); int block_numx=ceil((xOr+1.0)/16); int block_numy=ceil((yOr+1.0)/16); double blockSrc[256]={0}; int newcount=0; //int k=0,l=0; //分塊存儲進數組blockSrc[]中 for(int k=0;k<16;k++) { for(int l=0;l<16;l++) { int oldcount=(16*block_numy-16+k)*iWidth+16*block_numx-16+l; blockSrc[newcount]=pbSrc[oldcount]; newcount++; } }//分塊存入完畢
OPENCV,別人的分成4×4塊的c++代碼:
int main() { char* imgPath = "..\\img2.jpg"; //[1] 獲取圖片數據,並轉化成灰度圖 Mat img = imread(imgPath); if (NULL == img.data) { printf("read img error!\n"); exit(1); } Mat imgGray; cvtColor(img, imgGray, CV_BGR2GRAY); // [2] 獲取圖像相關信息 int nHeight = imgGray.rows; int nWidth = imgGray.cols; unsigned char* pData = imgGray.data; // [3] 將圖像分成4*4塊 int p = 0, q = 0; //用來標識塊索引 for (int ss = 0; ss < 16; ss++) { p = ss / 4; //行索引 q = ss % 4; //列索引 for (int i = nHeight*p / 4; i < nHeight*(p + 1) / 4; i++) { for (int j = nWidth*q / 4; j < nWidth*(q + 1) / 4;j++) { pData[i*nWidth + j] = ss * 255 / 16; //每一塊顯示一種顏色,用於可視化驗證 } } } //[4] 顯示圖像 namedWindow("img"); imshow("img", imgGray); waitKey(0); return 0; }
分塊讀寫策略:
一般而言圖像文件在磁盤上是按行存貯的,就是從第一行到最后一行的
將數據讀取/寫入到某一塊時,首先是從塊的起始地址開始,將塊的第一行的數據讀取/寫入。
但是,塊的第二行數據和第一行數據地址不是連續的。
【轉載自】
將圖像進行分塊(筆記) - CSDN博客 https://blog.csdn.net/a200800170331/article/details/54667188
略論圖像的分塊讀寫策略 - CSDN博客 https://blog.csdn.net/clever101/article/details/2651139
【其他】
[python + opencv] 圖像的旋轉和分塊 - CSDN博客 https://blog.csdn.net/Foolishwolf_x/article/details/39343011