按行或列读RAW图像并显示
任务描述:
设计一函数,该函数对K个M*N的灰度图像(矩阵)处理。功能是取每个图像的第X行(或列),生成一新的图像。要求用指针实现上述程序。
代码实现:
读取图像:
#include <fstream>
#include <iostream>
#include <cmath>
using namespace std
;
#define width 512
#define height 512
#define num 512
int main(){
unsigned short *data
;
unsigned short *data_new
;
int L
= width
* height
* num
;
int L_new
= width
* num
;
data
= new unsigned short[L
];
data_new
= new unsigned short[L_new
];
ifstream infile
;
infile
.open("D:\\data\\白蚁-CNU008269-head 512x512x512-ushort.raw",ios
::binary
);
infile
.read((char*)data
,L
* sizeof(unsigned short));
infile
.close();
cout
<< "输入所选行数(>0)或列数(<0):" << endl
;
int n
;
cin
>> n
;
if (n
>=0){
int a
= (n
-1) * height
;
for (int i
=0;i
<num
;i
++){
for (int j
=0;j
<width
;j
++){
*(data_new
+ j
+ width
* i
) = *(data
+ a
+ L_new
* i
+ j
);
}
}
}
else{
int b
= abs(n
);
for (int k
=0;k
<L
;k
++){
if (k
% width
== b
){
int t
= k
/width
;
*(data_new
+ t
) = *(data
+ k
);
}
}
}
ofstream outfile
;
outfile
.open("D:\\pythonProject\\pic_new.raw",ios
::binary
);
outfile
.write((char*)data_new
,L_new
* sizeof(unsigned short));
outfile
.close();
delete[] data
;
delete[] data_new
;
return 0;
}
显示图像:
import cv2
import numpy
as np
rows
=512
cols
=512
channels
=1
img
=np
.fromfile
('pic_new.raw', dtype
='uint16')
img
=img
.reshape
(rows
, cols
, channels
)
cv2
.imshow
('ans',img
)
cv2
.waitKey
()
cv2
.destroyAllWindows
()
print('ok')
结果示例:
第200行:
第200列:
转载请注明原文地址:https://blackberry.8miu.com/read-17105.html