Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include<iostream> #include<fstream> #include<utility> using namespace std; #inc

ID: 3539842 • Letter: #

Question

#include<iostream>

#include<fstream>

#include<utility>

using namespace std;


#include "stack_3358.h"


const int MAX = 25;


void showArray(const char array[][MAX], int rows, int columns);


int main() {


cout << "Please enter name of file containing "image": " << endl;

string fname;

cin >> fname;

ifstream fin ( fname.c_str() );

if ( !fin.is_open() ) {

cout<<"Could not open file ";

return 2;

}

  

// set up and input the "image"

char image [MAX][MAX];

  

string line;

int row = 0;

int column = 0;

while (getline(fin, line))

{

for (int i=0; i<line.size(); i++) {

image[row][i] = line[i];

}

row++;

column = line.size();

}


// display the image

showArray(image,row,column);

  

Stack_3358<char> s;

Stack_3358<int> mystack;// set up the stack

if(mystack.isEmpty())

cout<<"It is empty"<<endl;

else

cout<<"Not empty"<<endl;// ADD: define your stack variable here

// stack to track points

  

int changeRow; // row to change

int changeCol; // column to change

char changeColor;

  

cout << "Enter a row: ";

cin >> changeRow;

cout << "Enter a column: ";

cin >> changeCol;

cout << "Enter a new color: ";

cin >> changeColor;

  

// change another point and its region

while (changeRow!=-1 && changeCol!=-1) {

// ADD: save the color at (changeRow,changeCol)

  

// ADD: if the changeColor is different from the exisiting color at

// (changeRow, changeCol), then push the point (changeRow,changeCol)

// onto the stack.


// ADD:

// while the stack is not empty

  

// ADD: pop the next set of coordinates (x,y)

  

  

// ADD: for each (valid) neighbor (i,j) of (x,y),

// if the color at (i,j) needs to be changed,

// change the color at (i,j) and push (i,j) on the stack

  

}

  

showArray(image,row,column);

  

cout << "Enter a row: ";

cin >> changeRow;

cout << "Enter a column: ";

cin >> changeCol;

cout << "Enter a new color: ";

cin >> changeColor;

}

  

  

}


void showArray(const char array[][MAX], int rows, int columns) {

  

for (int x = 0; x < rows; x++) {

for (int y = 0; y < columns; y++)

cout << array[x][y];

cout << endl;

}

}

Explanation / Answer

package blobdetector;


import java.awt.Point;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.LinkedList;

import java.util.Queue;

import javax.imageio.ImageIO;

import javax.management.Query;


public class Main {


public Main() {

}


public static boolean isBlack(BufferedImage image, int posX, int posY) {


int color = image.getRGB(posX, posY);


int brightness = (color & 0xFF) + ((color >> 2) & 0xFF)

+ ((color >> 4) & 0xFF);

brightness /= 3;


return brightness < 128;

}


public static void main(String[] args) {

if (args.length != 1) {

System.err.println("ERROR: Pass filename as argument.");

return;

}


String filename = args[0];

// String filename =

// "C:\Users\Natthawut\Desktop\blob.jpg";

try {

BufferedImage bimg = ImageIO.read(new File(filename));


boolean[][] painted = new boolean[bimg.getHeight()][bimg.getWidth()];


for (int i = 0; i < bimg.getHeight(); i++) {

for (int j = 0; j < bimg.getWidth(); j++) {


if (isBlack(bimg, j, i) && !painted[i][j]) {


Queue<Point> queue = new LinkedList<Point>();

queue.add(new Point(j, i));


int pixelCount = 0;

while (!queue.isEmpty()) {

Point p = queue.remove();


if ((p.x >= 0)

&& (p.x < bimg.getWidth() && (p.y >= 0) && (p.y < bimg

.getHeight()))) {

if (!painted[p.y][p.x]

&& isBlack(bimg, p.x, p.y)) {

painted[p.y][p.x] = true;

pixelCount++;


queue.add(new Point(p.x + 1, p.y));

queue.add(new Point(p.x - 1, p.y));

queue.add(new Point(p.x, p.y + 1));

queue.add(new Point(p.x, p.y - 1));

}

}

}

System.out.println("Blob detected : " + pixelCount

+ " pixels");

}


}

}


} catch (IOException ex) {

ex.printStackTrace();

}


}


}