我正在做一项家庭作业,我们得到了一个 .bin 文件,其中包含灰度图像的数据。这里的目标是编写 C 代码,将其从灰度图像转换为二进制图像。第一件事是在 MATLAB 中打开灰度图像,我设法通过以下方式完成:
A = fread(file, 256*256, 'uint8=>uint8');
A = reshape(A, 256, 256).';
imshow(A);
太棒了。我有我的图像系数,我将其分成 4 个数组并填充到 C 头文件中。我必须这样做,因为我们用于此类的微 Controller - MSP430 - 无法同时处理所有这些。我导入了第一个头文件,设置了变量,所有繁重的工作都完成了:
for (i = 0; i == 16384; i++){
if (suzi[i] >= 95){ //95 is given threshhold
temp = 0b0000000;
}
else{
temp = 0b00000001 << shiftby; //will right shift by 7, then 6, then 5 until 0 and then wrap back to 7
}
current = previous | temp; //updates the 8 bit data with the latest bit
if(shiftby > 0){
shiftby--; //decrements shifts until 0
}
else{
shiftby = 7; //wraps back to 7
suziarr[arrayindex] &= 0b00000000; //makes sure this value is 8 0s
suziarr[arrayindex] |= current; //sets bits to match current 8 bit value
current = 0b00000000;
arrayindex++; //this is why the array will be 1/8 the size
}
previous = current; //update state
}
temp、suzi、suziarr、previous 和 current 变量被声明为 unsigned char 类型。 suzi 也是一个 const,是我塞到头文件中的数组之一。 suzi 设置为长度 13684,suziarr 设置为长度 2048。无论出于何种原因,无论我使用 4 组数据中的哪一组,我都会得到完全相同的数字,所以这里出了问题。我错过了什么?
最佳答案
循环
for (i = 0; i == 16384; i++)
不会迭代。应该是
for (i = 0; i < 16384; i++)
关于使用 MATLAB 和 C 将 .bin 转换为图像系数并创建二值图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36715670/