help for bmp2dta

Module for converting bitmap files to Stata datasets

Syntax

bmp2dta using filename, picture(filename) [ stub(string) replace]

Description

Since the advent of satellite imagery, researchers have turned images into data and processed it. This program makes the first step easier in Stata,
by turning a Windows 24-bit bitmap file into a Stata dataset with variables i and j (row and column of each pixel), r (red), g (green), and b (blue) on a scale of 0 to 255.

Examples of uses for satellite images include projecting fishing productivity (Kemmerer et al. 1976), estimating economic
growth (Henderson, Storeygard, and Weil 2012), and predicting poverty rates (Jean et al. 2016).

Techniques for processing, filtering, or otherwise manipulating image data can also be used on any photograph.

Examples

You will need to get the image files referred to below in order to use them in the following examples.

net describe bmp2dta, from(http://fmwww.bc.edu/RePEc/bocode/b)

net get bmp2dta

bmp2dta using bmore, pic(bm500.bmp)

u bmore, clear

sc i j if r<39&g<39&b<39, m(S) mcol(edkblue)msize(small) m(S) xsize(5) ysize(5) yla(1/200 ,nogrid) xsc(off) ysc(off) leg(off) xla(1/200) graphr(fc(white) margin(zero) lw(none)) plotr(margin(zero)) scale(.3)

forv i=1/3 {

su r if c==`i', mean

loc c`i' "`=round(r(mean))'"

su g if c==`i', mean

loc c`i' "`c`i'' `=round(r(mean))'"

su b if c==`i', mean

loc c`i' "`c`i'' `=round(r(mean))'"

}

sc i j if c==1, msize(small) m(S) mcol("`c1'")||sc i j if c==2, msize(small) m(S) mcol("`c2'%40")||sc i j if c==3, msize(vtiny) m(S) mcol("`c3'%20") xsize(5) ysize(5) yla(1/200 ,nogrid) xsc(off) ysc(off) leg(off) xla(1/200) graphr(fc(white) margin(zero) lw(none)) plotr(margin(zero)) scale(.2)

bmp2dta using dc276, pic(bmdc276.bmp)

u dc276, clear

cluster kmeans r g b, k(3) gen(c)

forv i=1/3 {

su r if c==`i', mean

loc c`i' "`=round(r(mean))'"

su g if c==`i', mean

loc c`i' "`c`i'' `=round(r(mean))'"

su b if c==`i', mean

loc c`i' "`c`i'' `=round(r(mean))'"

}

sc i j if c==1, msize(small) m(S) mcol("`c1'")||sc i j if c==2, msize(small) m(S) mcol("`c2'%40")||sc i j if c==3, msize(vtiny) m(S) mcol("`c3'%20")||sc i j if c==4, msize(vtiny) m(S) mcol("`c4'%20") xsize(5) ysize(5) yla(1/200 ,nogrid) xsc(off) ysc(off) leg(off) xla(1/200) graphr(fc(white) margin(zero) lw(none)) plotr(margin(zero)) scale(.2)

bmp2dta using putin, pic(bmputin.bmp)

u putin, clear

loc i 1

foreach c in "29 82 97" "86 151 163" "245 255 201" "161 30 34" "97 10 29" {

g d`i'=(r-real(word("`c'",1)))^2+(g-real(word("`c'",2)))^2+(g-real(word("`c'",3)))^2

loc i=`i'+1

}

g color=1 if d1==min(d1,d2,d3,d4,d5)

qui forv i=2/5 {

replace color=`i' if d`i'==min(d1,d2,d3,d4,d5)

}

keep if inrange(j,250,1600)

sc i j if color==2, msize(vtiny) m(S) mcol("86 151 163")||sc i j if color==1, msize(vtiny) m(S) mcol("29 82 97")||sc i j if color==3, msize(vtiny) m(S) mcol("245 255 201")||sc i j if color==4, msize(vtiny) m(S) mcol("161 30 34")||sc i j if color==5, msize(vtiny) m(S) mcol("97 10 29") xsize(5) ysize(4) yla(1/500,nogrid) xsc(off) ysc(off) leg(off) xla(250/500) graphr(fc(white) margin(zero))

Options summary

picture(filename) specifies the picture file to be turned into a dataset.

stub(string) specifies that variables i, j, r, g, and b in the using dataset should all be prefixed .

replace specifies that the using dataset may be overwritten.

References

Henderson, J. Vernon, Adam Storeygard, and David N. Weil. 2012. "Measuring Economic Growth from Outer Space." American Economic Review, 102(2): 994–1028.

Jean, Neal, Marshall Burke, Michael Xie, W. Matthew Davis, David B. Lobell, and Stefano Ermon. 2016. "Combining satellite imagery and machine learning to predict poverty." Science, 353(6301): 790-4.

Kemmerer, Andrew J., Kenneth J. Savastano, and Kenneth Faller. 1976. "LANDSAT Menhaden and Thread Herring Resources Investigation." Slidell, Louisiana: NASA.

Citation of bmp2dta

bmp2dta is not an official Stata command. It is a free contribution
to the research community, like a paper. Please cite it as such:

Nichols, Austin. 2007.
bmp2dta: Module for converting bitmap files to datasets.
http://ideas.repec.org/c/boc/bocode/s456890.html

Author

Austin Nichols
Washington, DC, USA
austinnichols@gmail.com

Also see

Manual: discrim and cluster commands}

On-line: help for (if installed) tddens.