@@ -83,7 +83,7 @@ savefig("projected_model.png",bbox_inches="tight")
8383# define constraint
8484constraint = Vector {SetIntersectionProjection.set_definitions} ()
8585
86- # slope constraints (lateral)
86+ # l2 constraint on gradient (lateral)
8787m_min = 0.0
8888m_max = 20.0
8989set_type = " l2"
@@ -104,3 +104,146 @@ figure();imshow(permutedims(reshape(m,(comp_grid.n[1],comp_grid.n[2])),[2,1]),cm
104104savefig (" original_model.png" ,bbox_inches= " tight" )
105105figure ();imshow (permutedims (reshape (x2,(comp_grid. n[1 ],comp_grid. n[2 ])),[2 ,1 ]),cmap= " jet" ,vmin= vmi,vmax= vma,extent= [0 , xmax, zmax, 0 ]); title (" Projection (l2 constraint on lateral derivative" )
106106savefig (" projected_model.png" ,bbox_inches= " tight" )
107+
108+
109+ # #########################################################################
110+ # # Constraint 4: Histogram constraints ##
111+
112+ using TestImages
113+ using SetIntersectionProjection
114+ using PyPlot
115+
116+ # goal observe histogram of mandril and make the cameraman have the same histogram
117+ mandril = testimage (" mandril_gray" )
118+ mandril = convert (Array{Float32,2 },mandril)
119+ cameraman = testimage (" cameraman" )
120+ cameraman = convert (Array{Float32,2 },cameraman)
121+
122+ # observe sorted values of mandril
123+ obs = sort (vec (mandril))
124+
125+ output = project_histogram_relaxed! (vec (deepcopy (cameraman)),obs,obs)
126+ output = reshape (output,size (cameraman))
127+
128+ figure ();
129+ subplot (2 ,3 ,1 );imshow (mandril);title (" madril image" )
130+ subplot (2 ,3 ,2 );imshow (cameraman);title (" cameraman image" )
131+ subplot (2 ,3 ,3 );imshow (output);title (" Cameraman image with histogram of mandril" )
132+ subplot (2 ,3 ,4 );hist (vec (mandril));title (" madril histogram" )
133+ subplot (2 ,3 ,5 );hist (vec (cameraman));title (" cameraman histogram" )
134+ subplot (2 ,3 ,6 );hist (vec (output));title (" adjusted cameraman histogram" )
135+
136+ # now set this up again, this time using the higher level tools in SetIntersectionProjection
137+ comp_grid = compgrid ((TF (1.0 ), TF (1.0 )),(size (cameraman,1 ), size (cameraman,2 )))
138+
139+ # construct permutation matrix
140+ # P1 y <= P2 x <= P1 y
141+ # <=>
142+ # P2' P1 y <= x <= P2' P1 y
143+ Id = spdiagm (0 => ones (prod (comp_grid. n)) )
144+ P = Id[sortperm (vec (cameraman)),:]
145+
146+ # define constraint
147+ constraint = Vector {SetIntersectionProjection.set_definitions} ()
148+
149+ # histogram constraints
150+ m_min = P' * obs
151+ m_max = P' * obs
152+ set_type = " bounds"
153+ TD_OP = " identity"
154+ app_mode = (" matrix" ," " )
155+ custom_TD_OP = ([],false )
156+ push! (constraint, set_definitions (set_type,TD_OP,m_min,m_max,app_mode,custom_TD_OP));
157+
158+ options. parallel = false
159+ (P_sub,TD_OP,set_Prop) = setup_constraints (constraint,comp_grid,options. FL);
160+ (TD_OP,AtA,l,y) = PARSDMM_precompute_distribute (TD_OP,set_Prop,comp_grid,options);
161+
162+ @time (x4,log_PARSDMM) = PARSDMM (deepcopy (vec (cameraman)),AtA,TD_OP,set_Prop,P_sub,comp_grid,options);
163+
164+ figure ();
165+ subplot (2 ,3 ,1 );imshow (mandril);title (" madril image" )
166+ subplot (2 ,3 ,2 );imshow (cameraman);title (" cameraman image" )
167+ subplot (2 ,3 ,3 );imshow (reshape (x4,comp_grid. n));title (" Cameraman image with histogram of mandril" )
168+ subplot (2 ,3 ,4 );hist (vec (mandril));title (" madril histogram" )
169+ subplot (2 ,3 ,5 );hist (vec (cameraman));title (" cameraman histogram" )
170+ subplot (2 ,3 ,6 );hist (vec (x4));title (" adjusted cameraman histogram" )
171+
172+ # # Constraint 5: Different constraints on different parameters ##
173+ # # as a toy example: different constraints on different channels of the RGB image
174+
175+ using Images
176+ using TestImages
177+ using SetIntersectionProjection
178+ using PyPlot
179+
180+ # goal observe histogram of mandril and make the cameraman have the same histogram
181+ mandril = testimage (" mandril" )
182+
183+ # now set this up again, this time using the higher level tools in SetIntersectionProjection
184+ comp_grid = compgrid ((TF (1.0 ), TF (1.0 )),(size (mandril,1 ), size (mandril,2 )))
185+
186+ mandril = channelview (mandril) # convert RGB to 3D array
187+
188+
189+ # define constraints
190+ constraint_channel_1 = Vector {SetIntersectionProjection.set_definitions} ()
191+ constraint_channel_2 = Vector {SetIntersectionProjection.set_definitions} ()
192+
193+ # l2 constraint on gradient for channel 1
194+ m_min = 0.0
195+ m_max = 3.0
196+ set_type = " l2"
197+ TD_OP = " D2D"
198+ app_mode = (" matrix" ," " )
199+ custom_TD_OP = ([],false )
200+ push! (constraint_channel_1, set_definitions (set_type,TD_OP,m_min,m_max,app_mode,custom_TD_OP));
201+
202+ # tv constraint for channel 2
203+ m_min = 0.0
204+ m_max = 200.0
205+ set_type = " l1"
206+ TD_OP = " D2D"
207+ app_mode = (" matrix" ," " )
208+ custom_TD_OP = ([],false )
209+ push! (constraint_channel_2, set_definitions (set_type,TD_OP,m_min,m_max,app_mode,custom_TD_OP));
210+
211+ # no constraints on channel 3
212+
213+ # set up projectors
214+ options. parallel = false
215+
216+ (P_sub1,TD_OP1,set_Prop1) = setup_constraints (constraint_channel_1,comp_grid,options. FL);
217+ (P_sub2,TD_OP2,set_Prop2) = setup_constraints (constraint_channel_2,comp_grid,options. FL);
218+ (TD_OP1,AtA1,l1,y1) = PARSDMM_precompute_distribute (TD_OP1,set_Prop1,comp_grid,options);
219+ (TD_OP2,AtA2,l2,y2) = PARSDMM_precompute_distribute (TD_OP2,set_Prop2,comp_grid,options);
220+
221+ # set up one function to project the separate RGB channels of the image onto different constraint sets
222+ proj_channel_1 = x-> PARSDMM (x, AtA1, TD_OP1, set_Prop1, P_sub1, comp_grid, options)
223+ proj_channel_2 = x-> PARSDMM (x, AtA2, TD_OP2, set_Prop2, P_sub2, comp_grid, options)
224+
225+ # Projection function
226+ function prj (input)
227+ input = Float32 .(input)
228+ output = deepcopy (input)
229+ (x1,dummy1,dummy2,dymmy3) = proj_channel_1 (vec (input[1 ,:,:]))
230+ (x2,dummy1,dummy2,dymmy3) = proj_channel_2 (vec (input[2 ,:,:]))
231+
232+ output[1 ,:,:] .= reshape (x1,size (input)[2 : 3 ])
233+ output[2 ,:,:] .= reshape (x2,size (input)[2 : 3 ])
234+ return output
235+ end
236+
237+ # project
238+ mandril_projected = prj (mandril)
239+
240+
241+ figure ();
242+ subplot (4 ,3 ,1 );imshow (permutedims (mandril,(2 ,3 ,1 )));title (" madril image" );colorbar ()
243+ subplot (4 ,3 ,4 );imshow (mandril[1 ,:,:]);title (" mandril ch1" );colorbar ()
244+ subplot (4 ,3 ,5 );imshow (mandril[2 ,:,:]);title (" mandril ch2" );colorbar ()
245+ subplot (4 ,3 ,6 );imshow (mandril[3 ,:,:]);title (" mandril ch3" );colorbar ()
246+ subplot (4 ,3 ,7 );imshow (permutedims (mandril_projected,(2 ,3 ,1 )));title (" projected madril image" );colorbar ()
247+ subplot (4 ,3 ,10 );imshow (mandril_projected[1 ,:,:]);title (" projected mandril ch1" );colorbar ()
248+ subplot (4 ,3 ,11 );imshow (mandril_projected[2 ,:,:]);title (" projected mandril ch2" );colorbar ()
249+ subplot (4 ,3 ,12 );imshow (mandril_projected[3 ,:,:]);title (" projected mandril ch3" );colorbar ()
0 commit comments