1

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

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 255 255 255] 类型不正确,应为图像

帮助?

谢谢!

4

2 回答 2

0

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等),但首先,让我们确保上述内容有效并且您了解它在做什么。

于 2013-03-06T19:05:46.040 回答
0

您将图像和颜色混为一谈(您正在调用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))
于 2013-03-06T17:23:36.910 回答