【发布时间】:2014-10-07 03:23:04
【问题描述】:
查看(解码的)png文件中的像素以确定哪个最暗哪个最亮应该不难,但是无论出于何种原因,当我过滤黑白图像时克里斯哈德菲尔德,我的函数告诉我最亮的像素是值 74,当明显有很多珍珠白像素时,它是深灰色,我的方法应该吐出 255。我有一个相反的 c 方法以及最暗的值和我按预期得到 0,但我的代码可能有什么问题。答案将不胜感激。在测试中,我将 lightest 设置为 255,因此任何像素都不可能比这更轻,我的代码仍然吐出 74。这是怎么回事?
uint8_t min( const uint8_t array[], unsigned int cols, unsigned int rows ) {
uint8_t darkest = 255;
for(int pixel = 0; pixel < (cols * rows); pixel++){
if(array[pixel] < darkest){
darkest = array[pixel]; }}
return darkest;
}
/* Return the lightest color that appears in the array; i.e. the
largest value
*/
uint8_t max( const uint8_t array[], unsigned int cols, unsigned int rows ) {
int8_t lightest = 0;
for(int pixel = 0; pixel < (cols * rows); pixel++){
if(array[pixel] > lightest){
lightest = array[pixel]; }}
return lightest;
}
【问题讨论】:
-
这里要小心有符号整数。全程尝试
uint8_t。 -
int8_t中的 255 为 -1。你在array[pixel] > lightest上没有收到警告吗? -
@Arkadiy:由于促销活动,最终将成为
int > int,这很好,没有警告。 -
我已经解决了这个问题,我眨了眨眼,意识到实际上我最轻的数据类型是错误的,应该是 uint8_t。谢谢
-
解决方案是什么?