doulan3436 2013-12-18 10:26
浏览 20
已采纳

当至少其中一个没有值时,使用php从图像中查找红色,绿色和蓝色数组的最大值

I'm working on a project that involves extracting Red, green and blue values from an image and finding the maximum value of red values, green values and blue values. When I upload an image that doesn't contain red values I got this error message: Undefined offset. The same message appears if image doesn't contain green or blue values. I used empty() to check if array is empty but it didn't help.

my code:

for($j1=0;$j1<=$red_count;$j1++){
    if (empty($reds)){
        $reds[$j1]=0;
    }
    if ($reds[$j1]>=$red_max){
        $red_max = $reds[$j1];
    }   
}

for($j2=0;$j2<=$blue_count;$j2++){
    if (empty($blues)){
        $blues[$j2]=0;
    }
    if ($blues[$j2]>=$blue_max){
        $blue_max = $blues[$j2];
    }
}

for($j3=0;$j3<=$green_count;$j3++){
    if (empty($greens)){
        $greens[$j3]=0;
    }
    if ($greens[$j3]>=$green_max){
        $green_max = $greens[$j3];
    }
}
  • 写回答

2条回答 默认 最新

  • dtq7387 2013-12-18 10:31
    关注

    Let's just look at one color. The problem is your for-loop. Because it is run once, even if $reds is empty. (And it's run on time to often.) Why not use foreach?

    $red_max = 0; //initialize
    foreach ($reds as $value) {
        if ($value > $red_max) { $red_max = $value; }
    }
    

    I guess you can apply this for the different colors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog