Skip to content

Commit

Permalink
add documenation for image read and write
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshksr committed Oct 10, 2023
1 parent 4b8655f commit f4b4026
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
OpenCV = "f878e3a2-a245-4720-8660-60795d644f2a"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
OpenCV = "f878e3a2-a245-4720-8660-60795d644f2a"
4 changes: 4 additions & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ DocMeta.setdocmeta!(OpenCV, :DocTestSetup, :(using OpenCV); recursive=true)
makedocs(;
modules=[OpenCV],
sitename="OpenCV.jl",
pages = [
"OpenCV.jl" => "index.md"
"Getting started with Images" => "Getting started with Images.md"
]
)

deploydocs(;
Expand Down
106 changes: 106 additions & 0 deletions docs/src/Getting started with Images.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Reading and Writing of Images

OpenCV provides a wide range of functions for image processing, including reading and writing images.

OpenCV.jl provides 2 ways to read and write images
1. OpenCV native api
2. FileIO.jl api

OpenCV provides `imread` and `imwrite`, while FileIO privides `load` and `save` for similar functionalities


!!! note

FileIO api not suppport all image formats currently but it support major formats.
If FileIO api not works then use OpenCV native api

## Reading Images

```julia
using OpenCV

img_path = "/path/to/image"
img = OpenCV.imread(img_path)
```
with FileIO
```julia
using OpenCV, FileIO

img_path = "/path/to/image"
img = load(img_path)
```

in both cases `img` has same data. Both methods accept same reading flags[^imreadflags].

```julia
using OpenCV

img_path = "/path/to/image"
flag = OpenCV.IMREAD_UNCHANGED
img = OpenCV.imread(img_path, flag)
```
with FileIO
```julia
using OpenCV, FileIO

img_path = "/path/to/image"
flag = OpenCV.IMREAD_UNCHANGED
img = load(img_path, flag)
```

## Writing Images
```julia
using OpenCV

img = rand(UInt8, 1000, 1000) |> OpenCV.Mat
img_path = "/path/to/image"
OpenCV.imwrite(img_path, img)
```
with FileIO
```julia
using OpenCV, FileIO

img = rand(UInt8, 1000, 1000) |> OpenCV.Mat
img_path = "/path/to/image"
save(img_path, img)
```

Both methods accept same writing flags[^imwriteflags].

```julia
using OpenCV

img = rand(UInt8, 1000, 1000) |> OpenCV.Mat
img_path = "/path/to/image"
flag = Int32[OpenCV.IMWRITE_JPEG_QUALITY, 100]
OpenCV.imwrite(img_path, img, flag)
```
with FileIO
```julia
using OpenCV, FileIO

img = rand(UInt8, 1000, 1000) |> OpenCV.Mat
img_path = "/path/to/image"
flag = Int32[OpenCV.IMWRITE_JPEG_QUALITY, 100]
save(img_path, img, flag)
```

## Displaying Images

When working with images, it's obviously helpful to be able to look at them. If you use Julia through Pluto, VSCode, or IJulia, images should display automatically.

Preview from Pluto.jl

![pluto preview](assets/pluto.png)

`OpenCV.imshow` method display image on Qt window.

![qt preview](assets/qt.png)

`Pluto.jl` To-do

`Makie.jl` To-do

## Notes
[^imreadflags]: https://docs.opencv.org/4.x/d8/d6a/group__imgcodecs__flags.html#ga61d9b0126a3e57d9277ac48327799c80
[^imwriteflags]: https://docs.opencv.org/4.x/d8/d6a/group__imgcodecs__flags.html#ga292d81be8d76901bff7988d18d2b42ac
Binary file added docs/src/assets/pluto.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/src/assets/qt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion src/fileio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ function load(f::File{T}, flags::Int) where {T<:_IMAGE_DATA_FORMATS}
return data
end

function load(s::Stream{T}, flags::Int=IMREAD_UNCHANGED) where {T<:_IMAGE_DATA_FORMATS}
function load(s::Stream{T}) where {T<:_IMAGE_DATA_FORMATS}
data = read(stream(s))
img = imdecode(reshape(data, 1, 1, :))
return img
end

function load(s::Stream{T}, flags::Int) where {T<:_IMAGE_DATA_FORMATS}
data = read(stream(s))
img = imdecode(reshape(data, 1, 1, :), flags)
return img
Expand Down

0 comments on commit f4b4026

Please sign in to comment.