x-stitch-pattern-converter_1


Information

Created with NetLogo version NetLogo 4.0.4
Running with NetLogoLite.jar version 404.


WHAT IS IT


This program offers the ability to convert an image into a cross-stitch pattern, including reducing the colors in the image to a set of available colors of embroidery floss.
Additionally the number of pixels (stitches) in the image is reduced to a maximum of 100 wide and 150 tall.
The program will list the floss colors used in the image, and the number of stiches in each color.
The resulting scaled image can be exported for printing.

HOW TO USE


To use all the features of this program, it is required to install the NetLogo programming language "package". This is an easy to install and use program that lets you run NetLogo programs and create your own programs in the NetLogo language.

IMPORT AN IMAGE


Click "browse" and select an image file. At this time, for best results, you should reduce the number of colors in the file to something near the number of floss colors you want to use. There are several free programs that will reduce the number of colors in an image for you. (Make a copy of the file, and reduce the colors in the copy, import the copy)
The file is "pixelated" to the number of stiches allowed by the program. If the image does not have the same proportions, it will be reduced in size to fit the available space.
The list of matching floss colors is generated.
The program can optionaly show or hide a grid of lines over the image to make the individual stitch "cells" more distinct. Adjust the grid-color slider to adjust the color of the grid lines.

TECHNICAL NOTES


DESCRIPTION OF ADAPTIVE SPACIAL SUBSDIVISION


The colors in the RGB color space are treated as if contained within a region of space where the red, green, and blue values (r,g,b) are treated as the x,y,z coordinates in three-dimensional space. Since the range of values for R, G, and B are each 0 - 255, the "color space" is a cube 256 units on a side.
Each pixel in the image is "placed" in this space according to its color. The cube is then devided into 8 identical sub cubes. The pixel is also places in the sub-cube that contains the pixel's coordinates. (the other sub-cubes don't really need to be created--only the cubes that are needed). Then this sub-cube is divided, and the pixel likewise placed. This continues until no further sub-divisions are possible (i.e., since the color space contains 2 ^ 8 values, up to 8 levels are used.
Each cube contains three bookkeeping values. N1 (pixels-contained) is the number of pixels that exist in this cube (i.e. including it's sub-cubes). N2 (pixels-exclusive) is the number of pixels that exist in this cube but in no sub-cube of this cube. Eq (quant-error) is the mean-difference of all the N2 pixels in this cube and the center of the cube. This is the amount of distortion of the color from the original.
The subcubes that have a N2 > 0 represent the colors required in the image.
By iterively promoting cubes with the lowest Ep up into their parent cubes, and calculating a new Eq for the parent cube, the number of cubes, and therefore the number of required colors, is decreased efficiently, while introducing the least quantization error.

DESCRIPTION OF CLASSIC K-MEANS COLOR REDUCTIONS


(Maybe IMPLEMENT AS ALTERNATE REDUCER--is it faster?)
1. Choose K points (centroids) in space
2. Assign each input vector to the nearest centroid
3. Recalculate positions of all centroids, so that the
new position of each centroid will be the average of
all vectors that have been assigned to this cluster
4. Go to step 2 until the positions of centroids no
longer move

Procedures

NetLogo Version: NetLogo 4.0.4

__includes
[ "floss-colors.nls"
  "color-reduction.nls"
  "rgb-utilities.nls"
  "floss-drawing.nls"
]

globals
[ flosses-in-use 
  original-colors
  some-line
  sampler-visible? ;; is the sampler icon in a visible state?
]

breed [ stitches a_stitch ]
breed [ lines line ]
breed [ samplers sampler ]


patches-own
[  ;; the input color for this stitch
  my-pixel ;; the pixel object that matches the input-color
  input-color
]

to startup setup end

to setup
   ca
   cp-rgb
   floss-setup
   sampler-setup
   set-default-shape stitches "x-stitch"
   draw-grid
end

to load-map [ #filename ]
   cp-rgb
   clear-stitches
   clear-pixels
   if #filename = "" [ set #filename user-file ]
   if is-string? #filename
   [ import-pcolors-rgb #filename
     ask patches [ set input-color pcolor ]
   ]
   setup-pixels
   update-floss-colors
   display
   clear-output
   output-print (word "There are " count pixels " colors in this image.")
   output-print (word "The imported image requires " count unique-floss-in-use " floss colors." )
   output-print "ASD color reduction is disabled."
   floss-used-report
end



to find-floss
   update-floss-colors
   
   floss-used-report
end

to clear-stitches
   ask stitches [ set pcolor [ color] of [my-floss] of my-pixel die ]
end   

to show-stitches
   ask patches
   [ let #color [ color ] of [my-floss] of my-pixel
     if not any? stitches-here
     [ sprout-stitches 1
       [ ]
     ]
     ask stitches-here
     [ set shape "x-stitch"
       set color #color ; fput 127 #color 
       __set-line-thickness thread-thickness
     ]
     set pcolor white
   ]
end   
   






to cp-rgb
   cp
   ask patches
   [ set pcolor [ 255 255 255 ]
     set input-color pcolor     
   ]
end


;;  to-report stitch-counts
;;     report map 
;;     [ 
;;       [ (word "#" code ":" label ": " count patches with [ stitch-floss = myself ] "\n")
;;       ] of ?
;;     ]
;;     sort (turtle-set [ stitch-floss] of patches)
;;  end

to-report count-unique-colors
  let map-cols 0; length remove-duplicates [ map-pcolor ] of patches
  let floss-cols 0 ; length remove-duplicates [ floss-color ] of patches
  let input-cols 0 ; length remove-duplicates [ input-color ] of patches
  report (word "i: " input-cols "  f: " floss-cols "  m: " map-cols )
end

to-report flosses-used
   report flosses-in-use ;; (turtle-set [ stitch-floss ] of patches )
end



   

to show-variance
   ask patches
   [ let i [ color ] of my-pixel
     let f [ color ] of [my-floss] of my-pixel
     let var (map [128 + .5 * (?1 - ?2)] f i )
     set pcolor var
   ]
end


   
 
 
to execute-color-reduction
   reduce-colors number-of-colors
   ask patches [ set pcolor [ map-color ] of my-pixel ]
end   


to draw-grid
   ask patches with [ pxcor != 0 and pycor = floor (world-height / 2) ]
   [ sprout-lines 1
     [ set shape "line-vert" set size world-height set color black set heading 0 set xcor xcor - .4]
     ]
     ask patches with [ pycor != 0 and pxcor = floor (world-width / 2) ]
   [ sprout-lines 1
     [ set shape "line-hori" set size world-width set color black set heading 0 set ycor ycor - .4 ]
   ]
   set some-line one-of lines
   
end
   
   
to-report update-grid-color
   let new-color item grid-color [ black white 5 15 25 35 45 55 65 75 85 95 105 115 125 135 ]
   if [ color ] of some-line != new-color
   [ ask lines [ set color new-color ] display ]  
   monitor-mouse
   report ""
end   

to monitor-mouse
   ifelse mouse-inside?
   [ ask samplers
     [ setxy floor mouse-xcor floor mouse-ycor
       set color pcolor
       set label [ label ] of patch-floss
       set label-color white - label-color
       show-turtle
       display
     ]
     set sampler-visible? true
   ]
   [ if sampler-visible? = true
     [ set sampler-visible? false
       ask samplers
       [ hide-turtle
         display
       ]
     ]
   ]
end   

to grid-toggle
   ask lines [ set hidden? not hidden? ]
end


to increase-contrast
let mnp min [ brightness ] of patches
let mxp max [ brightness ] of patches
let spanp mxp - mnp
let scalep 255 / spanp
if scalep < 1.0
[ ask patches [ set pcolor adjust-contrast pcolor mnp scalep ]
]
end


to-report brightness
   report mean pcolor
end

to-report adjust-contrast [ #rgb #min #scale ]
   report map [ (? - #min) * #scale ] #rgb
end
   
   
   
to export-image
   let #filename user-new-file
   if is-string? #filename
   [
     export-view #filename
   ]
end   

to sampler-setup
  create-samplers 1
  [ set shape "sample"
    set size 10
    setxy 0 0
    set hidden? true
  ]
end

  

                    


Download Link

View or download the complete model file (to download: right-click, save-link-as):
-- Download x-stitch-pattern-converter_1 --