SlideShare uma empresa Scribd logo
1 de 13
Baixar para ler offline
package VStabillization;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
public class PlayImage {
private int width = 480;
private int height = 270;
private int current = 0;
private int loadFrame = 100;
private int loadedFrame = -1;
private Object currentLock = new Object();
private final int intervalTime = 66;// 66;
private TimerTask updateFrameTimerTask;
private Thread imgs;
private Integer[] locks;
private BufferedImage[] bufferedImgs;
private int last = -1;
private String filename;
private int motionlessFrame = 0;
private double[] evaluateMotionResult;
private double[] evaluateSimilarityResult;
private double[] evaluateSimilarityByColorResult;
public FrameTransformation[] frameTransformationResult;
public FrameTransformation[] newTransformResult;
private int diffByColorThreshold = 100000;
private boolean processFinished = false;
private int[] frameNumberToPlay;
private double[] evaluateSenceChangeResult;
private int windowWidth = 30;
private BufferedImage[] adjustedImages;
private int debugProcessRatio = 1;
// peter
public int[][][] histogram = new int[4][4][4];
public int[][][] nextHistogram = new int[4][4][4];
public int frameRate = 15;
public int secForScene = 5;
public int SceneFrameCounter = 0;
public boolean findNextScene = false;
public int senceDiff = 0;
public int senceThreshold = 5000000;
public int[] sumFrame;
public int numOfFrame = 0;
public int sumIndex = 0;
public int[] senceChangeFrame;
public int senceChangeIndex = 0;
private boolean finished = false;
private BufferedImage compareImage;
// peter
public PlayImage(final String filename) {
this.filename = filename;
File file = new File(filename);
// long len = file.length();
long len = width * height * 3;
bufferedImgs = new BufferedImage[(int) (file.length() / len)];
// peter
sumFrame = new int[(int) (file.length() / len)];
senceChangeFrame = new int[(int) (file.length() / len)];
senceChangeFrame[0] = 0;
// peter
locks = new Integer[bufferedImgs.length];
for (int i = 0; i != locks.length; i++) {
locks[i] = new Integer(i);
}
Thread read = new Thread() {
public void run() {
PlayImage.this.allFrames(filename);
PlayImage.this.frameTransformationResult =
PlayImage.this.getEvaluateFrameTransformationResult();
PlayImage.this.newTransformResult =
PlayImage.this.getNewTransformResult(0,
frameTransformationResult.length/debugProcessRatio);
PlayImage.this.adjustedImages
=PlayImage.this.adjustFrames();
// PlayImage.this.evaluateSimilarityResult =
PlayImage.this.getEvaluateSimilarityResult();
// for (int i = 0; i !=
PlayImage.this.evaluateSimilarityResult.length; i++) {
// System.out.printf("%d: %fn", i,
PlayImage.this.evaluateSimilarityResult[i]);
// }
// int maxOrder =
PlayImage.this.findMaxOrder(PlayImage.this.evaluateSimilarityResult);
// PlayImage.this.evaluateSimilarityByColorResult =
PlayImage.this.getEvaluateSimilarityByColorResult(maxOrder);
//
// PlayImage.this.frameNumberToPlay =
PlayImage.this.generateFrameNumberToPlay();
// System.out.println("frameNumberToPlay");
// System.out.println(PlayImage.this.frameNumberToPlay[0]);
//// for (int i = 0; i !=
PlayImage.this.frameNumberToPlay.length; i++) {
////
System.out.println(PlayImage.this.frameNumberToPlay[i]);
//// }
frameNumberToPlay = new int[bufferedImgs.length-1];
for(int i = 0; i != frameNumberToPlay.length-1; i++){
frameNumberToPlay[i] = i;
}
CommunicateVariables communicateVariables =
CommunicateVariables.getSingular();
communicateVariables.imageIndexInput(frameNumberToPlay);
while(! communicateVariables.finished()){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//
// frameNumberToPlay = communicateVariables.getIndex();
//// for (int i = 0; i !=
PlayImage.this.frameNumberToPlay.length; i++) {
////
System.out.println(PlayImage.this.frameNumberToPlay[i]);
//// }
processFinished = true;
System.out.println(processFinished);
}
};
read.start();
}
public BufferedImage[] adjustFrames(){
EvaluateFrameTransformation evaluateFrameTransformation = new
EvaluateFrameTransformationByOpenCV();
BufferedImage[] result = new BufferedImage[bufferedImgs.length];
for(int i = 0 ; i!= bufferedImgs.length/debugProcessRatio; i++){
result[i] = adjustFrame(bufferedImgs[i],
newTransformResult[i]);
//result[i] =
evaluateFrameTransformation.applyTransformToImage(bufferedImgs[i],
newPrevToCurResult[i]);
}
return result;
}
public BufferedImage adjustFrame(BufferedImage img, FrameTransformation
frameTransformation){
BufferedImage img2 = new BufferedImage(img.getWidth(),
img.getHeight(), img.getType());
for(int i = 0; i != img.getWidth(); i++){
for(int j = 0; j != img.getHeight(); j++){
int tempX = (int) (i + frameTransformation.getDy());
int tempY = (int) (j + frameTransformation.getDx());
if( tempX >= 0 && tempX< img.getWidth() && tempY >= 0 &&
tempY < img.getHeight()){
img2.setRGB(tempX, tempY, img.getRGB(i, j));
}
}
}
return img2;
}
public double findMax(double[] value){
double max = value[0];
for(int i = 0; i!= value.length; i++){
if(max < value[i]){
max = value[i];
}
}
return max;
}
public int findMaxOrder(double[] value){
double max = value[0];
int order = 0;
for(int i = 0; i!= value.length; i++){
if(max < value[i]){
max = value[i];
order = i;
}
}
return order;
}
public int getCurrent(){
synchronized (currentLock) {
return this.current;
}
}
public double[] getSenceChangeResult(){
double[] result = new double[bufferedImgs.length];
List<Integer> temp = new ArrayList<>();
temp.add(0);
for(int i = 0; i != senceChangeFrame.length; i++){
if(senceChangeFrame[i] != 0){
temp.add(senceChangeFrame[i]);
System.out.println(senceChangeFrame[i]);
}
}
for(int i = 0; i != temp.size(); i++){
for(int j = 0 ; j != frameRate * secForScene; j++){
if(i+j < result.length){
result[temp.get(i)+j] = 500;
}
}
}
return result;
}
public int[] generateFrameNumberToPlay(double valve) {
double[] evaluateValue = getEvaluateValue();
int[] temp = new int[bufferedImgs.length];
int current = 0;
for (int i = 0; i != evaluateValue.length; i++) {
if (evaluateValue[i] > valve) {
temp[current] = i;
current++;
}
}
System.out.println(current);
return temp;
}
public int[] generateFrameNumberToPlay() {
double[] evaluateValue = getEvaluateValue();
int[] temp = new int[bufferedImgs.length];
int current = 0;
double max = -1000;
int order = 0;
for (int i = 0; i != evaluateValue.length; i++) {
if (evaluateValue[i] > max) {
max = evaluateValue[i];
order = i;
}
}
int start = order;
while(start > 1){
if(evaluateSimilarityByColorResult[start] >
diffByColorThreshold){
break;
}
else{
start-=1;
}
}
int end = order+1;
while(end < evaluateValue.length){
if(evaluateSimilarityByColorResult[end-1] >
diffByColorThreshold){
break;
}
else{
end+=1;
}
}
for(int i = 0; i != end-start; i++){
temp[i] = i+start;
}
// System.out.println(current);
System.out.printf("Start: %d", start);
System.out.printf("End: %d", end);
return temp;
}
public double[] getEvaluateValue() {
double[] result = new double[bufferedImgs.length];
for(int i = 0; i != result.length; i++){
result[i] = evaluateSimilarityResult[i];
}
return result;
}
public BufferedImage getCurrentImageProcessed() {
if (this.processFinished == false) {
return null;
}
synchronized (currentLock) {
if (current >= bufferedImgs.length) {
this.finished = true;
return null;
}
synchronized (locks[current]) {
if (last == current) {
return null;
} else {
last = current;
// return bufferedImgs[current];
return adjustedImages[current];
}
}
}
}
public double[] getEvaluateSimilarityByColorResult(int order){
EvaluateSimilarity evaluateSimilarity = new
EvaluateSimilarityByColor();
double[] result = new double[bufferedImgs.length];
result[0] = 0;
System.out.println("Color");
for (int i = 0; i < bufferedImgs.length; i++) {
result[i] = evaluateSimilarity.evaluateSimilarityBetweenImage(
bufferedImgs[i], bufferedImgs[order]);
System.out.printf("%d: %fn", i, result[i]);
}
return result;
}
public double[] getEvaluateSimilarityResult(){
EvaluateSimilarity evaluateSimilarity = new
EvaluateSimilarityByRescale();
double[] result = new double[bufferedImgs.length];
result[0] = 0;
for (int i = 0; i < bufferedImgs.length; i++) {
result[i] = evaluateSimilarity.evaluateSimilarityBetweenImage(
bufferedImgs[i], compareImage);
}
return result;
}
public FrameTransformation[] getEvaluateFrameTransformationResult(){
EvaluateFrameTransformation evaluateFrameTransformation = new
EvaluateFrameTransformationByOpenCV();
FrameTransformation[] result = new
FrameTransformation[bufferedImgs.length];
result[0] = new FrameTransformation(0, 0, 0);
for (int i = 1; i < bufferedImgs.length/debugProcessRatio; i++) {
BufferedImage temp, temp2;
synchronized (locks[i-1]) {
temp = bufferedImgs[i-1];
}
synchronized (locks[i]) {
temp2 = bufferedImgs[i];
}
result[i] =
evaluateFrameTransformation.evaluateMotionBetweenImage(
temp, temp2);
}
return result;
}
public FrameTransformation[] getNewTransformResult(int start, int end){
double a = 0;
double x = 0;
double y = 0;
List<FrameTransformation> path = new ArrayList<>();
for(int i = start; i< end; i++){
x += frameTransformationResult[i].getDx();
y += frameTransformationResult[i].getDy();
a += frameTransformationResult[i].getDa();
path.add(new FrameTransformation(x, y, a));
}
List<FrameTransformation> pathWithWindow = new ArrayList<>();
for(int i = 0; i < end-start; i++){
double xTotal = 0;
double yTotal = 0;
double aTotal = 0;
int number = 0;
for(int j=-windowWidth; j <= windowWidth; j++){
if(i+j>=0 && i+j < end-start){
xTotal += path.get(i+j).getDx();
yTotal += path.get(i+j).getDy();
aTotal += path.get(i+j).getDa();
number++;
}
}
double aAvg = aTotal /number;
double xAvg = xTotal/number;
double yAvg = yTotal / number;
pathWithWindow.add(new FrameTransformation(xAvg,yAvg, aAvg));
}
List<FrameTransformation> newFrameTransformation = new
ArrayList<>();
a = 0;
x = 0;
y = 0;
for(int i = 0; i < end-start; i++){
x+= frameTransformationResult[i+start].getDx();
y+= frameTransformationResult[i+start].getDy();
a+= frameTransformationResult[i+start].getDa();
double xDist = pathWithWindow.get(i).getDx() - x;
double yDist = pathWithWindow.get(i).getDy() - y;
double aDist = pathWithWindow.get(i).getDa() - a;
double dx = frameTransformationResult[i+start].getDx() +
xDist;
double dy = frameTransformationResult[i+start].getDy() +
yDist;
double da = frameTransformationResult[i+start].getDa() +
aDist;
newFrameTransformation.add(new FrameTransformation(dx, dy,
da));
}
FrameTransformation[] result = new
FrameTransformation[newFrameTransformation.size()];
for(int i = 0 ;i != result.length; i++){
result[i] = newFrameTransformation.get(i);
}
return result;
}
public double[] getEvaluateMotionResult() {
// EvaluateMotion evaluateMotion = new
EvaluateMotionByFramePredict(15,
// 15, 5, 5);
EvaluateMotion evaluateMotion = new
EvaluateMotionByCompareAverageValueInBlock(
15, 15, 500);
double[] result = new double[bufferedImgs.length];
result[0] = 0;
for (int i = 1; i < bufferedImgs.length; i++) {
BufferedImage temp, temp2;
synchronized (locks[i-1]) {
temp = bufferedImgs[i-1];
}
synchronized (locks[i]) {
temp2 = bufferedImgs[i];
}
result[i] = evaluateMotion.evaluateMotionBetweenImage(
temp, temp2);
}
double temp = 0;
for (int i = 0; i < bufferedImgs.length; i+= frameRate) {
for(int j = 0; j != frameRate; j++){
if(temp < result[i+j]){
temp= result[i+j];
}
}
for(int j = 0; j != frameRate; j++){
result[i+j] = temp;
}
}
return result;
}
// public BufferedImage readRightImg(){
//
// }
public BufferedImage getFirstImage() {
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
InputStream is = null;
File file = new File(this.filename);
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// long len = file.length();
long len = width * height * 3;
bufferedImgs = new BufferedImage[(int) (file.length() / len)];
byte[] bytes = new byte[(int) len];
int offset = 0;
int numRead = 0;
try {
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length
- offset)) >= 0) {
offset += numRead;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int ind = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
byte a = 0;
byte r = bytes[ind];
byte g = bytes[ind + height * width];
byte b = bytes[ind + height * width * 2];
int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff)
<< 8)
| (b & 0xff);
// int pix = ((a << 24) + (r << 16) + (g << 8) + b);
img.setRGB(x, y, pix);
ind++;
}
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return img;
}
public void allFrames(String filename) {
try {
File file = new File(filename);
InputStream is = new FileInputStream(file);
allFrames(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean isFinished(){
return finished;
}
public void allFrames(InputStream is) {
try {
for (int i = 0; i < bufferedImgs.length; i++) {
synchronized (locks[i]) {
if (bufferedImgs[i] == null) {
BufferedImage temp = readNextFrame(is);
bufferedImgs[i] = temp;
loadedFrame = i;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
public BufferedImage readNextFrame(InputStream is) throws IOException {
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// long len = file.length();
long len = width * height * 3;
byte[] bytes = new byte[(int) len];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length -
offset)) >= 0) {
offset += numRead;
}
int ind = 0;
if (findNextScene) {
SceneFrameCounter = 0;
// System.out.println("findNextScene" + findNextScene);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
byte a = 0;
byte r = bytes[ind];
byte g = bytes[ind + height * width];
byte b = bytes[ind + height * width * 2];
// peter
int red = r;
if (r < 0)
red = red + 256;
int green = g;
if (g < 0)
green = green + 256;
int blue = b;
if (b < 0)
blue = blue + 256;
r = (byte) red;
g = (byte) green;
b = (byte) blue;
nextHistogram[red / 64][green / 64][blue / 64]++;
int pix = 0xff000000 | ((r & 0xff) << 16)
| ((g & 0xff) << 8) | (b & 0xff);
// int pix = ((a << 24) + (r << 16) + (g << 8) +
b);
img.setRGB(x, y, pix);
ind++;
}
}
for (int i = 0; i < histogram.length; i++)
for (int j = 0; j < histogram[i].length; j++)
for (int p = 0; p < histogram[i][j].length; p++) {
senceDiff = Math.abs(histogram[i][j][p]
- nextHistogram[i][j][p]);
}
if (senceDiff >= senceThreshold) {
findNextScene = false;
for (int i = 0; i < histogram.length; i++)
for (int j = 0; j < histogram[i].length; j++)
for (int p = 0; p < histogram[i][j].length;
p++) {
histogram[i][j][p] = nextHistogram[i]
[j][p];
}
senceChangeIndex++;
senceChangeFrame[senceChangeIndex] = numOfFrame;
}
senceDiff = 0;
} else {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
byte a = 0;
byte r = bytes[ind];
byte g = bytes[ind + height * width];
byte b = bytes[ind + height * width * 2];
// peter
if (SceneFrameCounter >= frameRate * secForScene)
{
int red = r;
if (r < 0)
red = red + 256;
int green = g;
if (g < 0)
green = green + 256;
int blue = b;
if (b < 0)
blue = blue + 256;
r = (byte) red;
g = (byte) green;
b = (byte) blue;
histogram[red / 64][green / 64][blue / 64]+
+;
findNextScene = true;
}
// peter
// for(int i = 0; i < histogram.length; i++)
// for(int j = 0; j < histogram[i].length; j++)
// for(int p = 0; p < histogram[i][j].length; p++)
{
//
// }
int pix = 0xff000000 | ((r & 0xff) << 16)
| ((g & 0xff) << 8) | (b & 0xff);
// int pix = ((a << 24) + (r << 16) + (g << 8) +
b);
img.setRGB(x, y, pix);
ind++;
}
}
sumFrame[sumIndex] = numOfFrame;
sumIndex++;
SceneFrameCounter++;
// System.out.println(SceneFrameCounter);
}
numOfFrame++;
return img;
}
public BufferedImage getImg(int order) {
if (order >= bufferedImgs.length) {
return null;
}
synchronized (locks[order]) {
return bufferedImgs[order];
}
}
public BufferedImage getCurrentImg() {
if (this.processFinished == false) {
return null;
}
synchronized (currentLock) {
if (current >= bufferedImgs.length) {
this.finished = true;
return null;
}
synchronized (locks[current]) {
if (last == current) {
return null;
} else {
last = current;
return bufferedImgs[current];
// return adjustedImages[current];
}
}
}
}
public BufferedImage getCurrentImgScenery() {
synchronized (currentLock) {
if (current >= bufferedImgs.length) {
return null;
}
synchronized (locks[current]) {
if (last == current) {
return null;
} else {
last = current;
return bufferedImgs[sumFrame[current]];
}
}
}
}
public void start() {
PlayImage.this.current = 0;
if (updateFrameTimerTask != null) {
updateFrameTimerTask.cancel();
}
java.util.Timer updateFrameTimer = new java.util.Timer();
updateFrameTimerTask = new TimerTask() {
public void run() {
synchronized (PlayImage.this.currentLock) {
// System.out.printf("%d:
PlayImagen",PlayImage.this.current);
PlayImage.this.current++;
}
}
};
System.out.println("start");
updateFrameTimer.scheduleAtFixedRate(updateFrameTimerTask, 0,
PlayImage.this.intervalTime);
}
public void pause() {
if (updateFrameTimerTask != null) {
updateFrameTimerTask.cancel();
}
}
public void startOrContinue() {
if (updateFrameTimerTask == null) {
this.start();
} else {
this.avContinue();
}
}
public void avContinue() {
java.util.Timer updateFrameTimer = new java.util.Timer();
updateFrameTimerTask = new TimerTask() {
public void run() {
synchronized (PlayImage.this.currentLock) {
PlayImage.this.current++;
}
}
};
updateFrameTimer.scheduleAtFixedRate(updateFrameTimerTask, 0,
PlayImage.this.intervalTime);
}
}

Mais conteúdo relacionado

Mais procurados

Threads, Queues, and More: Async Programming in iOS
Threads, Queues, and More: Async Programming in iOSThreads, Queues, and More: Async Programming in iOS
Threads, Queues, and More: Async Programming in iOSTechWell
 
The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202Mahmoud Samir Fayed
 
C# Advanced L04-Threading
C# Advanced L04-ThreadingC# Advanced L04-Threading
C# Advanced L04-ThreadingMohammad Shaker
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Kuldeep Jain
 
The Ring programming language version 1.8 book - Part 87 of 202
The Ring programming language version 1.8 book - Part 87 of 202The Ring programming language version 1.8 book - Part 87 of 202
The Ring programming language version 1.8 book - Part 87 of 202Mahmoud Samir Fayed
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMRafael Winterhalter
 
The Ring programming language version 1.5.1 book - Part 9 of 180
The Ring programming language version 1.5.1 book - Part 9 of 180The Ring programming language version 1.5.1 book - Part 9 of 180
The Ring programming language version 1.5.1 book - Part 9 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 94 of 212
The Ring programming language version 1.10 book - Part 94 of 212The Ring programming language version 1.10 book - Part 94 of 212
The Ring programming language version 1.10 book - Part 94 of 212Mahmoud Samir Fayed
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ applicationDaniele Pallastrelli
 

Mais procurados (20)

Threads, Queues, and More: Async Programming in iOS
Threads, Queues, and More: Async Programming in iOSThreads, Queues, and More: Async Programming in iOS
Threads, Queues, and More: Async Programming in iOS
 
14 thread
14 thread14 thread
14 thread
 
Java Concurrency
Java ConcurrencyJava Concurrency
Java Concurrency
 
Elixir and OTP Apps introduction
Elixir and OTP Apps introductionElixir and OTP Apps introduction
Elixir and OTP Apps introduction
 
The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202The Ring programming language version 1.8 book - Part 31 of 202
The Ring programming language version 1.8 book - Part 31 of 202
 
分散式系統
分散式系統分散式系統
分散式系統
 
C# Advanced L04-Threading
C# Advanced L04-ThreadingC# Advanced L04-Threading
C# Advanced L04-Threading
 
concurrency_c#_public
concurrency_c#_publicconcurrency_c#_public
concurrency_c#_public
 
Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.Chat application in java using swing and socket programming.
Chat application in java using swing and socket programming.
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
The Ring programming language version 1.8 book - Part 87 of 202
The Ring programming language version 1.8 book - Part 87 of 202The Ring programming language version 1.8 book - Part 87 of 202
The Ring programming language version 1.8 book - Part 87 of 202
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVM
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Poly-paradigm Java
Poly-paradigm JavaPoly-paradigm Java
Poly-paradigm Java
 
Java.lang.object
Java.lang.objectJava.lang.object
Java.lang.object
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
The Ring programming language version 1.5.1 book - Part 9 of 180
The Ring programming language version 1.5.1 book - Part 9 of 180The Ring programming language version 1.5.1 book - Part 9 of 180
The Ring programming language version 1.5.1 book - Part 9 of 180
 
The Ring programming language version 1.10 book - Part 94 of 212
The Ring programming language version 1.10 book - Part 94 of 212The Ring programming language version 1.10 book - Part 94 of 212
The Ring programming language version 1.10 book - Part 94 of 212
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ application
 

Semelhante a Play image

Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Sunil Kumar Gunasekaran
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...DroidConTLV
 
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfimport java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfadhityalapcare
 
Hi, I need some one to help me with Design a client-server Chat so.pdf
Hi, I need some one to help me with Design a client-server Chat so.pdfHi, I need some one to help me with Design a client-server Chat so.pdf
Hi, I need some one to help me with Design a client-server Chat so.pdffashiongallery1
 
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...solit
 
please help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdfplease help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdfanfenterprises
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Domenic Denicola
 
C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴명신 김
 
Please help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfPlease help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfJUSTSTYLISH3B2MOHALI
 
Orsiso
OrsisoOrsiso
Orsisoe27
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introductioncaswenson
 
HTML5って必要?
HTML5って必要?HTML5って必要?
HTML5って必要?GCS2013
 
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docxC346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docxhumphrieskalyn
 

Semelhante a Play image (20)

Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfimport java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
 
Hi, I need some one to help me with Design a client-server Chat so.pdf
Hi, I need some one to help me with Design a client-server Chat so.pdfHi, I need some one to help me with Design a client-server Chat so.pdf
Hi, I need some one to help me with Design a client-server Chat so.pdf
 
Thread
ThreadThread
Thread
 
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
Solit 2013, Автоматизация тестирования сложных систем: mixed mode automated t...
 
please help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdfplease help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdf
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴C#을 이용한 task 병렬화와 비동기 패턴
C#을 이용한 task 병렬화와 비동기 패턴
 
Please help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfPlease help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdf
 
Orsiso
OrsisoOrsiso
Orsiso
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
JavaExamples
JavaExamplesJavaExamples
JavaExamples
 
HTML5って必要?
HTML5って必要?HTML5って必要?
HTML5って必要?
 
Java practical
Java practicalJava practical
Java practical
 
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docxC346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
C346_PA3_W12srccommonBaseThread.javaC346_PA3_W12srccommonB.docx
 
Griffon @ Svwjug
Griffon @ SvwjugGriffon @ Svwjug
Griffon @ Svwjug
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 

Último

How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxryandux83rd
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...Nguyen Thanh Tu Collection
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Osopher
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 

Último (20)

How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
CARNAVAL COM MAGIA E EUFORIA _
CARNAVAL COM MAGIA E EUFORIA            _CARNAVAL COM MAGIA E EUFORIA            _
CARNAVAL COM MAGIA E EUFORIA _
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptx
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
 
Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 

Play image

  • 1. package VStabillization; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.nio.Buffer; import java.util.ArrayList; import java.util.List; import java.util.TimerTask; public class PlayImage { private int width = 480; private int height = 270; private int current = 0; private int loadFrame = 100; private int loadedFrame = -1; private Object currentLock = new Object(); private final int intervalTime = 66;// 66; private TimerTask updateFrameTimerTask; private Thread imgs; private Integer[] locks; private BufferedImage[] bufferedImgs; private int last = -1; private String filename; private int motionlessFrame = 0; private double[] evaluateMotionResult; private double[] evaluateSimilarityResult; private double[] evaluateSimilarityByColorResult; public FrameTransformation[] frameTransformationResult; public FrameTransformation[] newTransformResult; private int diffByColorThreshold = 100000; private boolean processFinished = false; private int[] frameNumberToPlay; private double[] evaluateSenceChangeResult; private int windowWidth = 30; private BufferedImage[] adjustedImages; private int debugProcessRatio = 1; // peter public int[][][] histogram = new int[4][4][4]; public int[][][] nextHistogram = new int[4][4][4]; public int frameRate = 15; public int secForScene = 5; public int SceneFrameCounter = 0; public boolean findNextScene = false; public int senceDiff = 0; public int senceThreshold = 5000000; public int[] sumFrame; public int numOfFrame = 0; public int sumIndex = 0; public int[] senceChangeFrame; public int senceChangeIndex = 0; private boolean finished = false; private BufferedImage compareImage; // peter public PlayImage(final String filename) {
  • 2. this.filename = filename; File file = new File(filename); // long len = file.length(); long len = width * height * 3; bufferedImgs = new BufferedImage[(int) (file.length() / len)]; // peter sumFrame = new int[(int) (file.length() / len)]; senceChangeFrame = new int[(int) (file.length() / len)]; senceChangeFrame[0] = 0; // peter locks = new Integer[bufferedImgs.length]; for (int i = 0; i != locks.length; i++) { locks[i] = new Integer(i); } Thread read = new Thread() { public void run() { PlayImage.this.allFrames(filename); PlayImage.this.frameTransformationResult = PlayImage.this.getEvaluateFrameTransformationResult(); PlayImage.this.newTransformResult = PlayImage.this.getNewTransformResult(0, frameTransformationResult.length/debugProcessRatio); PlayImage.this.adjustedImages =PlayImage.this.adjustFrames(); // PlayImage.this.evaluateSimilarityResult = PlayImage.this.getEvaluateSimilarityResult(); // for (int i = 0; i != PlayImage.this.evaluateSimilarityResult.length; i++) { // System.out.printf("%d: %fn", i, PlayImage.this.evaluateSimilarityResult[i]); // } // int maxOrder = PlayImage.this.findMaxOrder(PlayImage.this.evaluateSimilarityResult); // PlayImage.this.evaluateSimilarityByColorResult = PlayImage.this.getEvaluateSimilarityByColorResult(maxOrder); // // PlayImage.this.frameNumberToPlay = PlayImage.this.generateFrameNumberToPlay(); // System.out.println("frameNumberToPlay"); // System.out.println(PlayImage.this.frameNumberToPlay[0]); //// for (int i = 0; i != PlayImage.this.frameNumberToPlay.length; i++) { //// System.out.println(PlayImage.this.frameNumberToPlay[i]); //// } frameNumberToPlay = new int[bufferedImgs.length-1]; for(int i = 0; i != frameNumberToPlay.length-1; i++){ frameNumberToPlay[i] = i; } CommunicateVariables communicateVariables = CommunicateVariables.getSingular(); communicateVariables.imageIndexInput(frameNumberToPlay); while(! communicateVariables.finished()){ try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block
  • 3. e.printStackTrace(); } } // // frameNumberToPlay = communicateVariables.getIndex(); //// for (int i = 0; i != PlayImage.this.frameNumberToPlay.length; i++) { //// System.out.println(PlayImage.this.frameNumberToPlay[i]); //// } processFinished = true; System.out.println(processFinished); } }; read.start(); } public BufferedImage[] adjustFrames(){ EvaluateFrameTransformation evaluateFrameTransformation = new EvaluateFrameTransformationByOpenCV(); BufferedImage[] result = new BufferedImage[bufferedImgs.length]; for(int i = 0 ; i!= bufferedImgs.length/debugProcessRatio; i++){ result[i] = adjustFrame(bufferedImgs[i], newTransformResult[i]); //result[i] = evaluateFrameTransformation.applyTransformToImage(bufferedImgs[i], newPrevToCurResult[i]); } return result; } public BufferedImage adjustFrame(BufferedImage img, FrameTransformation frameTransformation){ BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), img.getType()); for(int i = 0; i != img.getWidth(); i++){ for(int j = 0; j != img.getHeight(); j++){ int tempX = (int) (i + frameTransformation.getDy()); int tempY = (int) (j + frameTransformation.getDx()); if( tempX >= 0 && tempX< img.getWidth() && tempY >= 0 && tempY < img.getHeight()){ img2.setRGB(tempX, tempY, img.getRGB(i, j)); } } } return img2; } public double findMax(double[] value){ double max = value[0]; for(int i = 0; i!= value.length; i++){ if(max < value[i]){ max = value[i]; } } return max; } public int findMaxOrder(double[] value){ double max = value[0]; int order = 0; for(int i = 0; i!= value.length; i++){ if(max < value[i]){ max = value[i]; order = i;
  • 4. } } return order; } public int getCurrent(){ synchronized (currentLock) { return this.current; } } public double[] getSenceChangeResult(){ double[] result = new double[bufferedImgs.length]; List<Integer> temp = new ArrayList<>(); temp.add(0); for(int i = 0; i != senceChangeFrame.length; i++){ if(senceChangeFrame[i] != 0){ temp.add(senceChangeFrame[i]); System.out.println(senceChangeFrame[i]); } } for(int i = 0; i != temp.size(); i++){ for(int j = 0 ; j != frameRate * secForScene; j++){ if(i+j < result.length){ result[temp.get(i)+j] = 500; } } } return result; } public int[] generateFrameNumberToPlay(double valve) { double[] evaluateValue = getEvaluateValue(); int[] temp = new int[bufferedImgs.length]; int current = 0; for (int i = 0; i != evaluateValue.length; i++) { if (evaluateValue[i] > valve) { temp[current] = i; current++; } } System.out.println(current); return temp; } public int[] generateFrameNumberToPlay() { double[] evaluateValue = getEvaluateValue(); int[] temp = new int[bufferedImgs.length]; int current = 0; double max = -1000; int order = 0; for (int i = 0; i != evaluateValue.length; i++) { if (evaluateValue[i] > max) { max = evaluateValue[i]; order = i; } } int start = order; while(start > 1){ if(evaluateSimilarityByColorResult[start] >
  • 5. diffByColorThreshold){ break; } else{ start-=1; } } int end = order+1; while(end < evaluateValue.length){ if(evaluateSimilarityByColorResult[end-1] > diffByColorThreshold){ break; } else{ end+=1; } } for(int i = 0; i != end-start; i++){ temp[i] = i+start; } // System.out.println(current); System.out.printf("Start: %d", start); System.out.printf("End: %d", end); return temp; } public double[] getEvaluateValue() { double[] result = new double[bufferedImgs.length]; for(int i = 0; i != result.length; i++){ result[i] = evaluateSimilarityResult[i]; } return result; } public BufferedImage getCurrentImageProcessed() { if (this.processFinished == false) { return null; } synchronized (currentLock) { if (current >= bufferedImgs.length) { this.finished = true; return null; } synchronized (locks[current]) { if (last == current) { return null; } else { last = current; // return bufferedImgs[current]; return adjustedImages[current]; } } } } public double[] getEvaluateSimilarityByColorResult(int order){ EvaluateSimilarity evaluateSimilarity = new EvaluateSimilarityByColor(); double[] result = new double[bufferedImgs.length]; result[0] = 0;
  • 6. System.out.println("Color"); for (int i = 0; i < bufferedImgs.length; i++) { result[i] = evaluateSimilarity.evaluateSimilarityBetweenImage( bufferedImgs[i], bufferedImgs[order]); System.out.printf("%d: %fn", i, result[i]); } return result; } public double[] getEvaluateSimilarityResult(){ EvaluateSimilarity evaluateSimilarity = new EvaluateSimilarityByRescale(); double[] result = new double[bufferedImgs.length]; result[0] = 0; for (int i = 0; i < bufferedImgs.length; i++) { result[i] = evaluateSimilarity.evaluateSimilarityBetweenImage( bufferedImgs[i], compareImage); } return result; } public FrameTransformation[] getEvaluateFrameTransformationResult(){ EvaluateFrameTransformation evaluateFrameTransformation = new EvaluateFrameTransformationByOpenCV(); FrameTransformation[] result = new FrameTransformation[bufferedImgs.length]; result[0] = new FrameTransformation(0, 0, 0); for (int i = 1; i < bufferedImgs.length/debugProcessRatio; i++) { BufferedImage temp, temp2; synchronized (locks[i-1]) { temp = bufferedImgs[i-1]; } synchronized (locks[i]) { temp2 = bufferedImgs[i]; } result[i] = evaluateFrameTransformation.evaluateMotionBetweenImage( temp, temp2); } return result; } public FrameTransformation[] getNewTransformResult(int start, int end){ double a = 0; double x = 0; double y = 0; List<FrameTransformation> path = new ArrayList<>(); for(int i = start; i< end; i++){ x += frameTransformationResult[i].getDx(); y += frameTransformationResult[i].getDy(); a += frameTransformationResult[i].getDa(); path.add(new FrameTransformation(x, y, a)); } List<FrameTransformation> pathWithWindow = new ArrayList<>(); for(int i = 0; i < end-start; i++){ double xTotal = 0; double yTotal = 0; double aTotal = 0; int number = 0;
  • 7. for(int j=-windowWidth; j <= windowWidth; j++){ if(i+j>=0 && i+j < end-start){ xTotal += path.get(i+j).getDx(); yTotal += path.get(i+j).getDy(); aTotal += path.get(i+j).getDa(); number++; } } double aAvg = aTotal /number; double xAvg = xTotal/number; double yAvg = yTotal / number; pathWithWindow.add(new FrameTransformation(xAvg,yAvg, aAvg)); } List<FrameTransformation> newFrameTransformation = new ArrayList<>(); a = 0; x = 0; y = 0; for(int i = 0; i < end-start; i++){ x+= frameTransformationResult[i+start].getDx(); y+= frameTransformationResult[i+start].getDy(); a+= frameTransformationResult[i+start].getDa(); double xDist = pathWithWindow.get(i).getDx() - x; double yDist = pathWithWindow.get(i).getDy() - y; double aDist = pathWithWindow.get(i).getDa() - a; double dx = frameTransformationResult[i+start].getDx() + xDist; double dy = frameTransformationResult[i+start].getDy() + yDist; double da = frameTransformationResult[i+start].getDa() + aDist; newFrameTransformation.add(new FrameTransformation(dx, dy, da)); } FrameTransformation[] result = new FrameTransformation[newFrameTransformation.size()]; for(int i = 0 ;i != result.length; i++){ result[i] = newFrameTransformation.get(i); } return result; } public double[] getEvaluateMotionResult() { // EvaluateMotion evaluateMotion = new EvaluateMotionByFramePredict(15, // 15, 5, 5); EvaluateMotion evaluateMotion = new EvaluateMotionByCompareAverageValueInBlock( 15, 15, 500); double[] result = new double[bufferedImgs.length]; result[0] = 0; for (int i = 1; i < bufferedImgs.length; i++) { BufferedImage temp, temp2; synchronized (locks[i-1]) { temp = bufferedImgs[i-1]; } synchronized (locks[i]) {
  • 8. temp2 = bufferedImgs[i]; } result[i] = evaluateMotion.evaluateMotionBetweenImage( temp, temp2); } double temp = 0; for (int i = 0; i < bufferedImgs.length; i+= frameRate) { for(int j = 0; j != frameRate; j++){ if(temp < result[i+j]){ temp= result[i+j]; } } for(int j = 0; j != frameRate; j++){ result[i+j] = temp; } } return result; } // public BufferedImage readRightImg(){ // // } public BufferedImage getFirstImage() { BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); InputStream is = null; File file = new File(this.filename); try { is = new FileInputStream(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // long len = file.length(); long len = width * height * 3; bufferedImgs = new BufferedImage[(int) (file.length() / len)]; byte[] bytes = new byte[(int) len]; int offset = 0; int numRead = 0; try { while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } int ind = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) {
  • 9. byte a = 0; byte r = bytes[ind]; byte g = bytes[ind + height * width]; byte b = bytes[ind + height * width * 2]; int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); // int pix = ((a << 24) + (r << 16) + (g << 8) + b); img.setRGB(x, y, pix); ind++; } } try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return img; } public void allFrames(String filename) { try { File file = new File(filename); InputStream is = new FileInputStream(file); allFrames(is); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public boolean isFinished(){ return finished; } public void allFrames(InputStream is) { try { for (int i = 0; i < bufferedImgs.length; i++) { synchronized (locks[i]) { if (bufferedImgs[i] == null) { BufferedImage temp = readNextFrame(is); bufferedImgs[i] = temp; loadedFrame = i; } } } } catch (IOException e) { e.printStackTrace(); } finally { } } public BufferedImage readNextFrame(InputStream is) throws IOException {
  • 10. BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // long len = file.length(); long len = width * height * 3; byte[] bytes = new byte[(int) len]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } int ind = 0; if (findNextScene) { SceneFrameCounter = 0; // System.out.println("findNextScene" + findNextScene); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { byte a = 0; byte r = bytes[ind]; byte g = bytes[ind + height * width]; byte b = bytes[ind + height * width * 2]; // peter int red = r; if (r < 0) red = red + 256; int green = g; if (g < 0) green = green + 256; int blue = b; if (b < 0) blue = blue + 256; r = (byte) red; g = (byte) green; b = (byte) blue; nextHistogram[red / 64][green / 64][blue / 64]++; int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); // int pix = ((a << 24) + (r << 16) + (g << 8) + b); img.setRGB(x, y, pix); ind++; } } for (int i = 0; i < histogram.length; i++) for (int j = 0; j < histogram[i].length; j++) for (int p = 0; p < histogram[i][j].length; p++) { senceDiff = Math.abs(histogram[i][j][p] - nextHistogram[i][j][p]); } if (senceDiff >= senceThreshold) { findNextScene = false; for (int i = 0; i < histogram.length; i++) for (int j = 0; j < histogram[i].length; j++)
  • 11. for (int p = 0; p < histogram[i][j].length; p++) { histogram[i][j][p] = nextHistogram[i] [j][p]; } senceChangeIndex++; senceChangeFrame[senceChangeIndex] = numOfFrame; } senceDiff = 0; } else { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { byte a = 0; byte r = bytes[ind]; byte g = bytes[ind + height * width]; byte b = bytes[ind + height * width * 2]; // peter if (SceneFrameCounter >= frameRate * secForScene) { int red = r; if (r < 0) red = red + 256; int green = g; if (g < 0) green = green + 256; int blue = b; if (b < 0) blue = blue + 256; r = (byte) red; g = (byte) green; b = (byte) blue; histogram[red / 64][green / 64][blue / 64]+ +; findNextScene = true; } // peter // for(int i = 0; i < histogram.length; i++) // for(int j = 0; j < histogram[i].length; j++) // for(int p = 0; p < histogram[i][j].length; p++) { // // } int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); // int pix = ((a << 24) + (r << 16) + (g << 8) + b); img.setRGB(x, y, pix); ind++; } } sumFrame[sumIndex] = numOfFrame; sumIndex++; SceneFrameCounter++; // System.out.println(SceneFrameCounter); }
  • 12. numOfFrame++; return img; } public BufferedImage getImg(int order) { if (order >= bufferedImgs.length) { return null; } synchronized (locks[order]) { return bufferedImgs[order]; } } public BufferedImage getCurrentImg() { if (this.processFinished == false) { return null; } synchronized (currentLock) { if (current >= bufferedImgs.length) { this.finished = true; return null; } synchronized (locks[current]) { if (last == current) { return null; } else { last = current; return bufferedImgs[current]; // return adjustedImages[current]; } } } } public BufferedImage getCurrentImgScenery() { synchronized (currentLock) { if (current >= bufferedImgs.length) { return null; } synchronized (locks[current]) { if (last == current) { return null; } else { last = current; return bufferedImgs[sumFrame[current]]; } } } } public void start() { PlayImage.this.current = 0; if (updateFrameTimerTask != null) { updateFrameTimerTask.cancel(); } java.util.Timer updateFrameTimer = new java.util.Timer(); updateFrameTimerTask = new TimerTask() { public void run() {
  • 13. synchronized (PlayImage.this.currentLock) { // System.out.printf("%d: PlayImagen",PlayImage.this.current); PlayImage.this.current++; } } }; System.out.println("start"); updateFrameTimer.scheduleAtFixedRate(updateFrameTimerTask, 0, PlayImage.this.intervalTime); } public void pause() { if (updateFrameTimerTask != null) { updateFrameTimerTask.cancel(); } } public void startOrContinue() { if (updateFrameTimerTask == null) { this.start(); } else { this.avContinue(); } } public void avContinue() { java.util.Timer updateFrameTimer = new java.util.Timer(); updateFrameTimerTask = new TimerTask() { public void run() { synchronized (PlayImage.this.currentLock) { PlayImage.this.current++; } } }; updateFrameTimer.scheduleAtFixedRate(updateFrameTimerTask, 0, PlayImage.this.intervalTime); } }