Job Address: http://coursera.cs.princeton.edu/algs4/assignments/seamCarving.html
Job Difficulty:
1. How do I get the RGB properties of a graphic?
Need to study the picture, color class, etc., using getRGB (), getred (), Getgreen (), GetBlue () and other functions;
2, how to calculate the lowest energy curve from the top to the bottom (that is, the shortest path)?
It can be solved by using the Dijkstra algorithm in class;
3. Will the Findhorizontalseam and Findverticalseam methods be merged?
If you want to merge, the key problem is to transpose the energy matrix;
Easy to deduct points:
1, how to ensure that the object memory usage within the required memory range?
Unlike the first job, the data structure of the second part of the job is not shown. In this job, in order to reduce the memory usage of the object, we should try to avoid using the global private variables, so the parameters of some functions will become very long;
2, Removehorizontalseam and Removeverticalseam function after the execution of the object is not updated in time.
Be sure to update the image at the end of the two functions;
Part of the code:
1. Data structure:
Private int [] Colorrgb; Private int width; Private int height; Private Picture picture ; Private Static Final Double Max_energy = 1000.0;
2, solve the shortest path:
Private voidDijkstrasp (BooleanIsvertical,Double[] Energy,Double[] Distto,int[] edgeto) { intV = width *height; for(intv = 0; v < V; v++) {Distto[v]=double.positive_infinity; EDGETO[V]= 0; } intInitlen =width; if(!isvertical) Initlen =height; INDEXMINPQ<Double> PQ =NewIndexminpq<double>(V); for(intv = 0; v < Initlen; v++) {Distto[v]=Max_energy; Pq.insert (V, Distto[v]); } BooleanNotfinstat =true; while(!pq.isempty () &&Notfinstat) {Relax (Pq.delmin (), isvertical, Energy, Distto, Edgeto, PQ); for(inti = V-1; i > V-initlen; i--) if(Distto[i]! =double.positive_infinity) {Notfinstat=false; Break; } } } Private voidRelaxintVBooleanIsvertical,Double[] Energy,Double[] Distto,int[] Edgeto, indexminpq<double>PQ) { intx, Y, W; Doubleweight; intSeamwidth = width, seamheight =height; if(!isvertical) {Seamwidth=height; Seamheight=width; } x= v%Seamwidth; Y= V/Seamwidth; if(x = = 0 | | x = = SeamWidth-1 | | y = = seamHeight-1) return; for(intDelta =-1; Delta < 2; delta++) {W= (y+1) * seamwidth + (x +Delta); Weight= energy[x + delta][y + 1]; if(Distto[w] > Distto[v] +weight) {Distto[w]= Distto[v] +weight; EDGETO[W]=v; if(y + 1 = = seamHeight-1)return; if(Pq.contains (w)) Pq.changekey (W, distto[w]); ElsePq.insert (W, distto[w]); } } }
View Code
3. Solve the path of the top end:
Private int[] Findseam (BooleanIsvertical,Double[] Distto,int[] edgeto) { intMinindex = 0; DoubleMindist =double.positive_infinity; intSeamwidth = width, seamheight =height; if(!isvertical) {Seamwidth=height; Seamheight=width; } int[] Seampath =New int[Seamheight]; for(inti = 0; i < seamwidth; i++) if(distto[(seamHeight-1) * seamwidth + i] <mindist) {Minindex= (seamHeight-1) * seamwidth +i; Mindist= distto[(seamHeight-1) * seamwidth +i]; } for(inti = seamHeight-1; i > 0; i--) {Seampath[i]= Minindex%Seamwidth; Minindex=Edgeto[minindex]; } if(Seampath.length > 1) seampath[0] = seampath[1]; returnSeampath; }
View Code
4. Energy Transfer:
Private Double[] Energytranspose (intWintHBooleanistranspose) { Double[[] result =New Double[W][h]; for(inty = 0; Y < H; y++) for(intx = 0; x < W; X + +) { if(istranspose) Result[x][y] =Energy (y, x); ElseResult[x][y] =Energy (x, y); } returnresult; }
View Code
5, Removeverticalseam ():
Public voidRemoveverticalseam (int[] seam) { if(seam.length! = Height | | width <= 1) Throw Newjava.lang.IllegalArgumentException (); Checkseam (seam, width); int[] copy =New int[Width-1][height]; for(inty = 0; Y < height; y++) { for(intx = 0; x < width; X + +) { if(x < seam[y]) copy[x][y] =Colorrgb[x][y]; Else if(x > Seam[y]) copy[x-1][y] =Colorrgb[x][y]; }} Width--; Colorrgb=copy; Picture (); }
View Code
Princeton algorithm Class Part2 second week assignment _seamcarving