我如何计算 Scala 语言中所有的红色、绿色和蓝色?
How can I count all red and green and blue color in Scala language?
我在计算图片中每种颜色的 RGB 时遇到问题。首先,我从光盘加载图片,然后读取这张图片。但我不会分开颜色并计算有多少像素是红色、绿色或蓝色。
package com.szymonBikowski
import java.awt.Color
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import com.szymonBikowski.loadImage.getListOffiles
import org.w3c.dom.css.RGBColor
object photoAnalizer {
var path = "/home/biku/Pulpit/In"
var correctFile = "/home/biku/Pulpit/In/c.jpg"
var splitPath = correctFile.split("""/""")
var filePath = loadImage.getListOffiles(path)
var filename = splitPath(splitPath.length-1)
var correctFilePathToSave = "/home/biku/Pulpit/Out/" + filename
// for (v <- splitPath)
// {
// println(v)
// }
def photoReader(image: BufferedImage): BufferedImage = {
var numberOfRed = 0
var numberOfGreen = 0
var numberOfBlue = 0
val lightGreen = new Color(0,255,0)
val darkGreen = new Color(0,100,0)
// width and height load photo
val width = image.getWidth
val height = image.getHeight
// create new image with the same size like load photo
val imageOut = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
// copy pixels from load photo(horizontally)
for (x <- 0 until width)
for (y <- 0 until height) {
imageOut.setRGB(x,y, image.getRGB(width-x-1,y) & 0xffffff)
// count pixels R&G&B
val color = new Color(image.getRGB(x,y))
if(isBetween(color, lightGreen, darkGreen))
numberOfGreen += 1
}
imageOut
}
def isBetween(color: Color, color1: Color, color2: Color): Boolean = {
color.getRed >= color1.getRed && color.getRed <= color2.getRed &&
color.getBlue >= color1.getBlue && color.getBlue <= color2.getBlue &&
color.getGreen <= color1.getGreen && color.getGreen >= color2.getGreen
}
def imageLoad(): Unit ={
//read heiht and width from load photo
val photo = ImageIO.read(new File(correctFile))
val photo2 = photoReader(photo)
}
imageLoad()
def main(args: Array[String]): Unit = {
}
}
我尝试直接使用颜色,然后尝试使用上面的隔间。
请有人帮帮我好吗?
我认为您可以使用 Color 中的 getRed()、getGreen()、getBlue() 方法 class:
import java.awt.Color
import java.awt.image.BufferedImage
def photoReader(image: BufferedImage): BufferedImage = {
// width and height load photo
val width = image.getWidth
val height = image.getHeight
// create new image with the same size like load photo
val imageOut = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
// this is sequence of rgb for each pair x, y
val rgbs = for {
x <- 0 until width
y <- 0 until height
color = new Color(image.getRGB(x,y))
} yield (color.getRed, color.getGreen, color.getBlue)
// here you sum all red, green, blue values for all x, y pairs
val (numberOfRed, numberOfGreen, numberOfBlue) = rgbs.fold(0, 0, 0){
case ((sumRed, sumGreen, sumBlue), (red, green, blue)) =>
((sumRed + red), (sumBlue + blue), (sumGreen + green))
}
imageOut
}
更新:
yield 来自 for-comprehension 构造。它将循环迭代的结果收集到序列中(类似于列表),您可以指定表达式来收集您需要收集的内容。如果需要收集rgb-colors,可以收集每种颜色的tuple3,这段代码的结果:
for {
x <- 0 until width
y <- 0 until height
color = new Color(image.getRGB(x,y))
} yield (color.getRed, color.getGreen, color.getBlue)
将是元组序列,其中每个元素都是元组 - 每对 (x, y) 的 (red, green, blue)。之后,您可以使用 fold 函数对所有元组求和。它需要累加器,在我们的例子中是带有零的 tuple3 - (0, 0, 0) 并将序列的每个元素添加到它。
如果您需要获取 rgb,您可以通过 rgb 元组结束方法并更改签名,同时更改函数名称:
def getRGB(image: BufferedImage): (Int, Int, Int) = {
...
(numberOfRed, numberOfGreen, numberOfBlue)
}
我在计算图片中每种颜色的 RGB 时遇到问题。首先,我从光盘加载图片,然后读取这张图片。但我不会分开颜色并计算有多少像素是红色、绿色或蓝色。
package com.szymonBikowski
import java.awt.Color
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import com.szymonBikowski.loadImage.getListOffiles
import org.w3c.dom.css.RGBColor
object photoAnalizer {
var path = "/home/biku/Pulpit/In"
var correctFile = "/home/biku/Pulpit/In/c.jpg"
var splitPath = correctFile.split("""/""")
var filePath = loadImage.getListOffiles(path)
var filename = splitPath(splitPath.length-1)
var correctFilePathToSave = "/home/biku/Pulpit/Out/" + filename
// for (v <- splitPath)
// {
// println(v)
// }
def photoReader(image: BufferedImage): BufferedImage = {
var numberOfRed = 0
var numberOfGreen = 0
var numberOfBlue = 0
val lightGreen = new Color(0,255,0)
val darkGreen = new Color(0,100,0)
// width and height load photo
val width = image.getWidth
val height = image.getHeight
// create new image with the same size like load photo
val imageOut = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
// copy pixels from load photo(horizontally)
for (x <- 0 until width)
for (y <- 0 until height) {
imageOut.setRGB(x,y, image.getRGB(width-x-1,y) & 0xffffff)
// count pixels R&G&B
val color = new Color(image.getRGB(x,y))
if(isBetween(color, lightGreen, darkGreen))
numberOfGreen += 1
}
imageOut
}
def isBetween(color: Color, color1: Color, color2: Color): Boolean = {
color.getRed >= color1.getRed && color.getRed <= color2.getRed &&
color.getBlue >= color1.getBlue && color.getBlue <= color2.getBlue &&
color.getGreen <= color1.getGreen && color.getGreen >= color2.getGreen
}
def imageLoad(): Unit ={
//read heiht and width from load photo
val photo = ImageIO.read(new File(correctFile))
val photo2 = photoReader(photo)
}
imageLoad()
def main(args: Array[String]): Unit = {
}
}
我尝试直接使用颜色,然后尝试使用上面的隔间。 请有人帮帮我好吗?
我认为您可以使用 Color 中的 getRed()、getGreen()、getBlue() 方法 class:
import java.awt.Color
import java.awt.image.BufferedImage
def photoReader(image: BufferedImage): BufferedImage = {
// width and height load photo
val width = image.getWidth
val height = image.getHeight
// create new image with the same size like load photo
val imageOut = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
// this is sequence of rgb for each pair x, y
val rgbs = for {
x <- 0 until width
y <- 0 until height
color = new Color(image.getRGB(x,y))
} yield (color.getRed, color.getGreen, color.getBlue)
// here you sum all red, green, blue values for all x, y pairs
val (numberOfRed, numberOfGreen, numberOfBlue) = rgbs.fold(0, 0, 0){
case ((sumRed, sumGreen, sumBlue), (red, green, blue)) =>
((sumRed + red), (sumBlue + blue), (sumGreen + green))
}
imageOut
}
更新: yield 来自 for-comprehension 构造。它将循环迭代的结果收集到序列中(类似于列表),您可以指定表达式来收集您需要收集的内容。如果需要收集rgb-colors,可以收集每种颜色的tuple3,这段代码的结果:
for {
x <- 0 until width
y <- 0 until height
color = new Color(image.getRGB(x,y))
} yield (color.getRed, color.getGreen, color.getBlue)
将是元组序列,其中每个元素都是元组 - 每对 (x, y) 的 (red, green, blue)。之后,您可以使用 fold 函数对所有元组求和。它需要累加器,在我们的例子中是带有零的 tuple3 - (0, 0, 0) 并将序列的每个元素添加到它。 如果您需要获取 rgb,您可以通过 rgb 元组结束方法并更改签名,同时更改函数名称:
def getRGB(image: BufferedImage): (Int, Int, Int) = {
...
(numberOfRed, numberOfGreen, numberOfBlue)
}