如何计算Scala语言中的所有红色、绿色和蓝色?
我无法从图片中计算每种颜色的RGB。首先,我从光盘中加载图片,然后读取此图片。但我不能分开颜色,也不能计算有多少像素是红色、绿色或蓝色如何计算Scala语言中的所有红色、绿色和蓝色?,scala,image-processing,rgb,Scala,Image Processing,Rgb,我无法从图片中计算每种颜色的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.getLis
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 = {
}
}
package com.szymonBikowski
导入java.awt.Color
导入java.awt.image.buffereImage
导入java.io.xml文件
导入javax.imageio.imageio
导入com.szymonBikowski.loadImage.getListOffiles
导入org.w3c.dom.css.RGBColor
目标光分析仪{
var path=“/home/biku/Pulpit/In”
var correctFile=“/home/biku/Pulpit/In/c.jpg”
var splitPath=correctFile.split(“”/“”)
var filePath=loadImage.getListOffiles(路径)
var filename=splitPath(splitPath.length-1)
var correctFilePathToSave=“/home/biku/Pulpit/Out/”+文件名
//对于(v我认为您可以使用Color类中的getRed()、getGreen()、getBlue()方法:
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
}
你能解释一下我的案例中的“收益率案例”结构吗?我如何在其他方法中使用“sumRed+red”呢?我填好了答案。
for {
x <- 0 until width
y <- 0 until height
color = new Color(image.getRGB(x,y))
} yield (color.getRed, color.getGreen, color.getBlue)
def getRGB(image: BufferedImage): (Int, Int, Int) = {
...
(numberOfRed, numberOfGreen, numberOfBlue)
}