根据范围改变图像的颜色



我正在尝试制作一个使用图像映射根据范围改变图像颜色的程序。这样的:

If the sum of the RGB channels for one pixel = 0 to 181 then the color would be (0 51 76)
If the sum = 182 to 363 then the color would be (217 26 33)
If the sum = 364 to 545 then the color would be (112 150 158)
If the sum = 546 to 765 then the color would be (252 227 166)

现在,这是我到目前为止的内容:

(define (sum p)
  (image-map
   (lambda (c)
    (+ (color-ref c 'red) (color-ref c 'green) (color-ref c 'blue)))
   p))
(define color-range
  (lambda (c)
    (cond
      [(< (sum c) 181) (color 0 51 76)]
      [(and (>= (sum c) 182) (<= (sum c) 363)) (color 217 26 33)]
      [(and (>= (sum c) 364) (<= (sum c) 545)) (color 112 150 158)]
      [(and (>= (sum c) 546) (<= (sum c) 765)) (color 252 227 166)])))

所以,我做了一个辅助函数来计算每个像素的和。当我运行color-range时,我得到一个错误说:

image-map: #[color 255 255 255]类型不正确

帮助吗?

谢谢!

sum的预期输入是一个像素还是一个图像?如果它是一个像素,为什么要用image-map遍历它?如果它是一个图像,为什么要添加所有像素的所有颜色组件,并设置作为一个新的像素?

我相信这是更接近你的意图(我不能告诉你,只是目前在问题中的代码片段);还请注意,我修复了color-range中的几个错误:

(define sum
  (lambda (c)
    (+ (color-ref c 'red)
       (color-ref c 'green)
       (color-ref c 'blue))))
(define color-range
  (lambda (c)
    (cond
      [(<= 0   (sum c) 181) (color 0 51 76)]
      [(<= 182 (sum c) 363) (color 217 26 33)]
      [(<= 364 (sum c) 545) (color 112 150 158)]
      [else                 (color 252 227 166)])))
(define change-colors
  (lambda (image)
    (image-map (lambda (pixel)
                 (color-range pixel))
               image)))

当然,上面的可以进一步优化(例如,通过删除对sum的多个调用,并将color-range直接传递给image-map等),但首先,让我们确保上面的工作,并且您了解它在做什么。

您正在合并图像和颜色(您正在使用颜色调用sum,但sum期望图像,似乎)。更明确你的类型。例如:

(define (color-intensity c)
  (+ (color-ref c 'red) (color-ref c 'green) (color-ref c 'blue)))
(define (remap-color c)
  (let ((intensity (color-intensity c)))
    (cond [(<=   0 intensity 181) ...]
          [(<= 182 intensity 363) ...]
          ...)))
(define (remap-image i)
  (image-map remap-color i))

最新更新