I have problem with count each color RGB from picture. First I load picture from disc and next read this picture. But I con't separate colors and count how many pixels are red, green or blue.
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 = {
}
}
I try to do this directly colors and next try using compartments as above.
Somone help me please ?
I think you can use getRed(), getGreen(), getBlue() methods from Color 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
}
UPDATE:
yield is from for-comprehension construction. It collects result of loop iterations into sequence (something like list), and you can specify expression what you need to collect. If you need collect rgb-colors, you can collect tuple3 with each color, the result of this code:
for {
x <- 0 until width
y <- 0 until height
color = new Color(image.getRGB(x,y))
} yield (color.getRed, color.getGreen, color.getBlue)
will be sequence of tuples, where each element is tuple - (red, green, blue) for each pair of (x, y). After that you can sum all tuples using fold function. It takes accumulator, in our case is tuple3 with zeros - (0, 0, 0) and adds to it every element of sequence.
if you need get rgb you can end method by rgb tuple and change signature, change function name also:
def getRGB(image: BufferedImage): (Int, Int, Int) = {
...
(numberOfRed, numberOfGreen, numberOfBlue)
}
I have written a program to find the list for move required to cover all square of the chessboard using Warnsdorff’s algorithm. it is perfectly working for 7x7 board but not working for board like 8x8, 10x10 or 16x16. Its goes on running for long. The below are the code. Please point out where I am going wrong.
object PawnTourMain {
def main(args: Array[ String ]): Unit = {
val kt = PawnTour(7)
kt.findTour(0, 1, 0)
kt.printSolution
}
class PawnTour(size: Int, board: Array[ Array[ Int ] ], possibleMoves: Array[ Array[ Array[ Point ] ] ]) {
val UNUSED = -1
def findTour(x: Int, y: Int, current: Int): Boolean = {
if (board(x)(y) != UNUSED) return false
//Mark current position as 'current'
board(x)(y) = current
if (current == size * size - 1) { //done :)
return true
}
for (d <- possibleMoves(x)(y)) {
if (findTour(d.x, d.y, current + 1)) return true
}
//if we are here, all our options ran out :(
//reset the current cell and return false
board(x)(y) = UNUSED
false
}
def printSolution: Unit = {
board foreach {
row =>
row foreach (number => print(number + " ")); println
}
}
}
case class Point(x: Int, y: Int)
object PawnTour {
val DIRECTIONS = Array(Point(3, 0), Point(-3, 0), Point(2, -2), Point(2, 2), Point(0, 3), Point(0, -3), Point(-2, -2), Point(2, 2))
def apply(n: Int): PawnTour = {
val board = Array.fill[ Int ](n, n)(-1)
val possibleMoves = Array.tabulate(n, n) { (x, y) =>
DIRECTIONS.flatMap { d =>
val nx = x + d.x
val ny = y + d.y
if ((nx >= 0 && nx < n) && (ny >= 0 && ny < n)) Option(Point(nx, ny)) else None
}
}
var x = 0
while (x < n) {
var y = 0
while (y < n) {
val moves: Array[ Point ] = possibleMoves(x)(y)
moves.sortBy((o1: Point) => possibleMoves(o1.x)(o1.y).size)
y += 1
println(moves.toList)
}
x += 1
}
new PawnTour(n, board, possibleMoves)
}
def printSolution(array: Array[ Array[ Int ] ]): Unit = {
array foreach {
row =>
row foreach (number => print(number + " ")); println
}
}
}
}
From Wikipedia
The knight is moved so that it always proceeds to the square from which the knight will have the fewest onward moves
Your implementation does not take into account already visited squares. It creates and sorts possible moves on empty board but forgets to update them when algorithm makes a move.
When square is visited you should remove it from possible moves and then reorder possible moves again
I just started writing Scala about a month ago. I however have been writing in Java, Javascript and some others.
Please I'd need someone to tell me why this code wouldn't display the result of the move(x,y) method. Even though it builds and runs succesfully.
class PinPoint(val xc: Int, val yc: Int){
var x:Int = xc; var y:Int = yc
def move(dx:Int, dy:Int){
x = x + dx
y = y + dy
println("Position on horizontal axis is " + x);
print("Position on vertical axis is " + y);
}
}
object Run {
def main(args: Array[String]) {
val pos = new PinPoint(20,18);
println()
pos.move(5,7);
}
}
Consider also this approach where a new PinPoint instance is created each time move() is called, namely using (immutable) values instead of (mutable) variables,
case class PinPoint(x: Int, y: Int) {
def move(dx: Int, dy: Int) = PinPoint(x+dx, y+dy)
override def toString() =
s"""|Position on horizontal axis is $x
|Position on vertical axis is $y""".stripMargin
}
object Run {
def main(args: Array[String]) {
val pos = PinPoint(20,18);
println(pos)
val shiftedPos = pos.move(5,7);
println(shiftedPos)
}
}
Method toString() delivers a multiline string where class members x and y have been interpolated.
One way to compile and run it,
scalac PinPoint.scala -d PinPoint.jar
scala PinPoint.jar
and so
Position on horizontal axis is 20
Position on vertical axis is 18
Position on horizontal axis is 25
Position on vertical axis is 25
I have a problem implementing Conrad Parker's boids pseudocode.
I'm implementing rule1, rule2 and rule3. The problem is that whenever rule3 is active (ie, matchSpeed in my code below), the boids rush to the centre of the world (0, 0, 0) and then flock around that spot. This happens regardless of where they start in the world.
But when rule3 doesn't run, the boids flock and drift as expected. What am I doing wrong?
My code is in Scala and I'm using the jMonkeyEngine, but I suspect the problem is a general one.
val sepDistance = 10f
val clumpFactor = 100f
val avoidFactor = 3f
val alignFactor = 800f
val speedLimit = 2f
def moveAgents(target: Node)
{
agents.foreach(a => {
a.velocity.addLocal(clump(a)) //rule1
a.velocity.addLocal(keepAway(a)) //rule2
a.velocity.addLocal(matchSpeed(a)) //rule3
a.velocity = limitSpeed(a.velocity)
a.move(a.velocity)
})
}
def clump (a: Agent): Vector3f = // rule1
{
val centre = Vector3f.ZERO.clone
for (oA <- agents if oA != a) yield
centre.addLocal(oA.position)
centre.divideLocal(agents.length.toFloat - 1f)
centre.subtractLocal(a.position)
centre.divideLocal(clumpFactor)
return centre
}
def keepAway (a: Agent): Vector3f = // rule2
{
val keepAway = Vector3f.ZERO.clone
for (oA <- agents if oA != a) {
if (Math.abs(oA.position.distance(a.position)) < sepDistance)
keepAway.subtractLocal(oA.position.subtract(a.position))
}
return keepAway.divide(avoidFactor)
}
def matchSpeed (a: Agent): Vector3f = // rule3
{
val matchSpeed = Vector3f.ZERO.clone
for (oA <- agents if oA != a)
matchSpeed.addLocal(oA.velocity)
matchSpeed.divideLocal(agents.length.toFloat - 1f)
matchSpeed.subtractLocal(a.position)
matchSpeed.divideLocal(alignFactor)
return matchSpeed
}
The problem is that the matchSpeed method subtracts the focal boid's position from the average velocity, rather than its velocity.
So:
matchSpeed.subtractLocal(a.position)
Should be:
matchSpeed.subtractLocal(a.velocity)
I'm trying to make a simple one top fifteen with this code:
var top = Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
val numbers = Array(4,5,8,1,33,23,45,6,11,10,87,46,43,66,55,98,78,71,19,20)
def getTop() : Unit = {
for ( i <- 0 to (numbers.length - 1)) {
set(0, numbers(i), top)
}
top.map(x => println(x))
}
def set(index: Int, number: Int, top: Array[Int]) : Unit = {
if (index <= top.length-1) {
if(top(index) < number) {
top(index) = number
}
else {
set(index+1, number, top)
}
}
}
The function should copy the top fifteen elements that are in the array numbers to array top, but currently I get only:
98,78,71,20,0,0,0,0,0,0,0,0,0,0,0
What am I doing wrong?
Your code doesn't work because you overriding previously set numbers:
top = Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) //initially
top = Array(4,0,0,0,0,0,0,0,0,0,0,0,0,0,0) // let's call set for '4'
top = Array(5,0,0,0,0,0,0,0,0,0,0,0,0,0,0) // now for 5 ...
top = Array(8,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
top = Array(8,1,0,0,0,0,0,0,0,0,0,0,0,0,0)
....
You can fix it with adjacent placing number you're replacing to the right place:
val top = Array.fill(15)(0)
val numbers = Array(4,5,8,1,33,23,45,6,11,10,87,46,43,66,55,98,78,71,19,20)
def printTop() : Unit = {
for (n <- numbers) {
set(0, n, top)
}
top.foreach(x => println(x))
}
def set(index: Int, number: Int, top: Array[Int]) : Unit = {
if (index < top.length) {
val current = top(index)
if(current < number) {
top(index) = number
set(index+1, current, top) // send replaced number down the street
}
else {
set(index+1, number, top)
}
}
}