How to reproduce image using writeJPEG from the mean of all elements in a nested array in Rconvert list to a matrix or array?How can I remove an element from a list?How do I install an R package from source?How to find common elements from multiple vectors?How to make a great R reproducible exampleHow can I remove all objects but one from the workspace in R?How to save a plot as image on the disk?Reproduce table and plot from journalSelect first element of nested listMean of all means of subsets of data differs from overall meanHow to calculate the mean by index of N elements in an array in R

What standard algorithm can determine if exactly one of a container satisfies a predicate?

Russian word for a male zebra

60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race

How creative should the DM let an artificer be in terms of what they can build?

Should I refuse being named as co-author of a bad quality paper?

How do free-speech protections in the United States apply in public to corporate misrepresentations?

How can one's career as a reviewer be ended?

Can the removal of a duty-free sales trolley result in a measurable reduction in emissions?

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

Insert external file and modify each line from script

What is the logic behind taxing money for property?

Ability To Change Root User Password (Vulnerability?)

I have a problematic assistant manager, but I can't fire him

How can I remove material from this wood beam?

Increase speed altering column on large table to NON NULL

The Frozen Wastes

Next date with distinct digits

What are some really overused phrases in French that are common nowadays?

Is it possible for a vehicle to be manufactured without a catalytic converter?

Electricity free spaceship

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

Will Roalesk, Apex Hybrid, trigger Sharktocrab twice?

Did Apple bundle a specific monitor with the Apple II+ for schools?

Can a human be transformed into a Mind Flayer?



How to reproduce image using writeJPEG from the mean of all elements in a nested array in R


convert list to a matrix or array?How can I remove an element from a list?How do I install an R package from source?How to find common elements from multiple vectors?How to make a great R reproducible exampleHow can I remove all objects but one from the workspace in R?How to save a plot as image on the disk?Reproduce table and plot from journalSelect first element of nested listMean of all means of subsets of data differs from overall meanHow to calculate the mean by index of N elements in an array in R






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have a nested array with a complicated structure. This array is a collection of 15 people, each with 11 images taken portraying 11 facial expressions such as wink, happy, etc.



I would like to calculate the mean images of 15 people with their 11 images taken.



enter image description here



The structure of all.faces is:



> str(all.faces)
List of 165
$ : int [1:159, 1:159] 48 50 51 50 50 53 57 55 50 47 ...
$ : int [1:159, 1:159] 159 156 131 130 117 115 122 119 123 129 ...
(... and 163 more...)
[list output truncated]
- attr(*, "dim")= int [1:2] 11 15
- attr(*, "dimnames")=List of 2
..$ : chr [1:11] "centerlight" "glasses" "happy" "leftlight" ...
..$ : NULL


When I tried



myarray <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))

mean.faces <- apply(myarray,1:2, mean)
writeJPEG(mean.faces,"MEAN_FACES.jpg")


I got this:



enter image description here



How come? I would assume that I would some sort of image, not a mess of black and white color scheme like this.



Reproducible example



faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
faces.array <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))

mean.all.faces <- apply(faces.array,1:2, mean)
writeJPEG(mean.all.faces,"MEAN_FACES.jpg")


What is the best way to approach this situation?



Thanks in advance.










share|improve this question
























  • Your apply would work, except that would require your data to be in an 3d array, not a list.

    – Axeman
    Mar 24 at 19:56











  • @Axeman How can I handle this case with a list?

    – Adam Ralphus
    Mar 24 at 19:57






  • 1





    @AdamRalphus See if this helps stackoverflow.com/a/37436412/6574038

    – jay.sf
    Mar 24 at 19:59











  • @jay.sf I updated my post.

    – Adam Ralphus
    Mar 24 at 20:18






  • 1





    Can you share a minimal reproducible example?

    – markus
    Mar 24 at 20:27

















1















I have a nested array with a complicated structure. This array is a collection of 15 people, each with 11 images taken portraying 11 facial expressions such as wink, happy, etc.



I would like to calculate the mean images of 15 people with their 11 images taken.



enter image description here



The structure of all.faces is:



> str(all.faces)
List of 165
$ : int [1:159, 1:159] 48 50 51 50 50 53 57 55 50 47 ...
$ : int [1:159, 1:159] 159 156 131 130 117 115 122 119 123 129 ...
(... and 163 more...)
[list output truncated]
- attr(*, "dim")= int [1:2] 11 15
- attr(*, "dimnames")=List of 2
..$ : chr [1:11] "centerlight" "glasses" "happy" "leftlight" ...
..$ : NULL


When I tried



myarray <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))

mean.faces <- apply(myarray,1:2, mean)
writeJPEG(mean.faces,"MEAN_FACES.jpg")


I got this:



enter image description here



How come? I would assume that I would some sort of image, not a mess of black and white color scheme like this.



Reproducible example



faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
faces.array <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))

mean.all.faces <- apply(faces.array,1:2, mean)
writeJPEG(mean.all.faces,"MEAN_FACES.jpg")


What is the best way to approach this situation?



Thanks in advance.










share|improve this question
























  • Your apply would work, except that would require your data to be in an 3d array, not a list.

    – Axeman
    Mar 24 at 19:56











  • @Axeman How can I handle this case with a list?

    – Adam Ralphus
    Mar 24 at 19:57






  • 1





    @AdamRalphus See if this helps stackoverflow.com/a/37436412/6574038

    – jay.sf
    Mar 24 at 19:59











  • @jay.sf I updated my post.

    – Adam Ralphus
    Mar 24 at 20:18






  • 1





    Can you share a minimal reproducible example?

    – markus
    Mar 24 at 20:27













1












1








1


1






I have a nested array with a complicated structure. This array is a collection of 15 people, each with 11 images taken portraying 11 facial expressions such as wink, happy, etc.



I would like to calculate the mean images of 15 people with their 11 images taken.



enter image description here



The structure of all.faces is:



> str(all.faces)
List of 165
$ : int [1:159, 1:159] 48 50 51 50 50 53 57 55 50 47 ...
$ : int [1:159, 1:159] 159 156 131 130 117 115 122 119 123 129 ...
(... and 163 more...)
[list output truncated]
- attr(*, "dim")= int [1:2] 11 15
- attr(*, "dimnames")=List of 2
..$ : chr [1:11] "centerlight" "glasses" "happy" "leftlight" ...
..$ : NULL


When I tried



myarray <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))

mean.faces <- apply(myarray,1:2, mean)
writeJPEG(mean.faces,"MEAN_FACES.jpg")


I got this:



enter image description here



How come? I would assume that I would some sort of image, not a mess of black and white color scheme like this.



Reproducible example



faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
faces.array <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))

mean.all.faces <- apply(faces.array,1:2, mean)
writeJPEG(mean.all.faces,"MEAN_FACES.jpg")


What is the best way to approach this situation?



Thanks in advance.










share|improve this question
















I have a nested array with a complicated structure. This array is a collection of 15 people, each with 11 images taken portraying 11 facial expressions such as wink, happy, etc.



I would like to calculate the mean images of 15 people with their 11 images taken.



enter image description here



The structure of all.faces is:



> str(all.faces)
List of 165
$ : int [1:159, 1:159] 48 50 51 50 50 53 57 55 50 47 ...
$ : int [1:159, 1:159] 159 156 131 130 117 115 122 119 123 129 ...
(... and 163 more...)
[list output truncated]
- attr(*, "dim")= int [1:2] 11 15
- attr(*, "dimnames")=List of 2
..$ : chr [1:11] "centerlight" "glasses" "happy" "leftlight" ...
..$ : NULL


When I tried



myarray <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))

mean.faces <- apply(myarray,1:2, mean)
writeJPEG(mean.faces,"MEAN_FACES.jpg")


I got this:



enter image description here



How come? I would assume that I would some sort of image, not a mess of black and white color scheme like this.



Reproducible example



faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
faces.array <- array(as.numeric(unlist(faces)), dim=c(159, 159, 15))

mean.all.faces <- apply(faces.array,1:2, mean)
writeJPEG(mean.all.faces,"MEAN_FACES.jpg")


What is the best way to approach this situation?



Thanks in advance.







r computation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 21:20







Adam Ralphus

















asked Mar 24 at 19:54









Adam RalphusAdam Ralphus

11410




11410












  • Your apply would work, except that would require your data to be in an 3d array, not a list.

    – Axeman
    Mar 24 at 19:56











  • @Axeman How can I handle this case with a list?

    – Adam Ralphus
    Mar 24 at 19:57






  • 1





    @AdamRalphus See if this helps stackoverflow.com/a/37436412/6574038

    – jay.sf
    Mar 24 at 19:59











  • @jay.sf I updated my post.

    – Adam Ralphus
    Mar 24 at 20:18






  • 1





    Can you share a minimal reproducible example?

    – markus
    Mar 24 at 20:27

















  • Your apply would work, except that would require your data to be in an 3d array, not a list.

    – Axeman
    Mar 24 at 19:56











  • @Axeman How can I handle this case with a list?

    – Adam Ralphus
    Mar 24 at 19:57






  • 1





    @AdamRalphus See if this helps stackoverflow.com/a/37436412/6574038

    – jay.sf
    Mar 24 at 19:59











  • @jay.sf I updated my post.

    – Adam Ralphus
    Mar 24 at 20:18






  • 1





    Can you share a minimal reproducible example?

    – markus
    Mar 24 at 20:27
















Your apply would work, except that would require your data to be in an 3d array, not a list.

– Axeman
Mar 24 at 19:56





Your apply would work, except that would require your data to be in an 3d array, not a list.

– Axeman
Mar 24 at 19:56













@Axeman How can I handle this case with a list?

– Adam Ralphus
Mar 24 at 19:57





@Axeman How can I handle this case with a list?

– Adam Ralphus
Mar 24 at 19:57




1




1





@AdamRalphus See if this helps stackoverflow.com/a/37436412/6574038

– jay.sf
Mar 24 at 19:59





@AdamRalphus See if this helps stackoverflow.com/a/37436412/6574038

– jay.sf
Mar 24 at 19:59













@jay.sf I updated my post.

– Adam Ralphus
Mar 24 at 20:18





@jay.sf I updated my post.

– Adam Ralphus
Mar 24 at 20:18




1




1





Can you share a minimal reproducible example?

– markus
Mar 24 at 20:27





Can you share a minimal reproducible example?

– markus
Mar 24 at 20:27












1 Answer
1






active

oldest

votes


















1














Short answer: writeJPEG() needs a matrix of values between 0 and 1. You're giving it the raw data which has values in the range 0-255.



In detail, the example you gave (yalefaces.mat) has dimensions:



> faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
> dim(faces[[1]])
[1] 48 42 2414


so it's actually 2414 faces of size 48x42. We can display one like so:



> image(faces[[1]][,,1])


a face



If you average them (without trying to resize the array, of course):



> mean.all.faces <- apply(faces[[1]], 1:2, mean)
> image(mean.all.faces)


it seems to work fine:



enter image description here



But when you write it out to a JPEG, the following will give garbage:



> writeJPEG(mean.all.faces, "BAD.jpg")


enter image description here



while the following works (though it's a little dark):



> writeJPEG(mean.all.faces/255, "GOOD.jpg")


enter image description here






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327950%2fhow-to-reproduce-image-using-writejpeg-from-the-mean-of-all-elements-in-a-nested%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Short answer: writeJPEG() needs a matrix of values between 0 and 1. You're giving it the raw data which has values in the range 0-255.



    In detail, the example you gave (yalefaces.mat) has dimensions:



    > faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
    > dim(faces[[1]])
    [1] 48 42 2414


    so it's actually 2414 faces of size 48x42. We can display one like so:



    > image(faces[[1]][,,1])


    a face



    If you average them (without trying to resize the array, of course):



    > mean.all.faces <- apply(faces[[1]], 1:2, mean)
    > image(mean.all.faces)


    it seems to work fine:



    enter image description here



    But when you write it out to a JPEG, the following will give garbage:



    > writeJPEG(mean.all.faces, "BAD.jpg")


    enter image description here



    while the following works (though it's a little dark):



    > writeJPEG(mean.all.faces/255, "GOOD.jpg")


    enter image description here






    share|improve this answer



























      1














      Short answer: writeJPEG() needs a matrix of values between 0 and 1. You're giving it the raw data which has values in the range 0-255.



      In detail, the example you gave (yalefaces.mat) has dimensions:



      > faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
      > dim(faces[[1]])
      [1] 48 42 2414


      so it's actually 2414 faces of size 48x42. We can display one like so:



      > image(faces[[1]][,,1])


      a face



      If you average them (without trying to resize the array, of course):



      > mean.all.faces <- apply(faces[[1]], 1:2, mean)
      > image(mean.all.faces)


      it seems to work fine:



      enter image description here



      But when you write it out to a JPEG, the following will give garbage:



      > writeJPEG(mean.all.faces, "BAD.jpg")


      enter image description here



      while the following works (though it's a little dark):



      > writeJPEG(mean.all.faces/255, "GOOD.jpg")


      enter image description here






      share|improve this answer

























        1












        1








        1







        Short answer: writeJPEG() needs a matrix of values between 0 and 1. You're giving it the raw data which has values in the range 0-255.



        In detail, the example you gave (yalefaces.mat) has dimensions:



        > faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
        > dim(faces[[1]])
        [1] 48 42 2414


        so it's actually 2414 faces of size 48x42. We can display one like so:



        > image(faces[[1]][,,1])


        a face



        If you average them (without trying to resize the array, of course):



        > mean.all.faces <- apply(faces[[1]], 1:2, mean)
        > image(mean.all.faces)


        it seems to work fine:



        enter image description here



        But when you write it out to a JPEG, the following will give garbage:



        > writeJPEG(mean.all.faces, "BAD.jpg")


        enter image description here



        while the following works (though it's a little dark):



        > writeJPEG(mean.all.faces/255, "GOOD.jpg")


        enter image description here






        share|improve this answer













        Short answer: writeJPEG() needs a matrix of values between 0 and 1. You're giving it the raw data which has values in the range 0-255.



        In detail, the example you gave (yalefaces.mat) has dimensions:



        > faces <- readMat("https://github.com/angelleng/545dump/blob/master/yalefaces.mat?raw=true")
        > dim(faces[[1]])
        [1] 48 42 2414


        so it's actually 2414 faces of size 48x42. We can display one like so:



        > image(faces[[1]][,,1])


        a face



        If you average them (without trying to resize the array, of course):



        > mean.all.faces <- apply(faces[[1]], 1:2, mean)
        > image(mean.all.faces)


        it seems to work fine:



        enter image description here



        But when you write it out to a JPEG, the following will give garbage:



        > writeJPEG(mean.all.faces, "BAD.jpg")


        enter image description here



        while the following works (though it's a little dark):



        > writeJPEG(mean.all.faces/255, "GOOD.jpg")


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 21:30









        K. A. BuhrK. A. Buhr

        17.3k21638




        17.3k21638





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327950%2fhow-to-reproduce-image-using-writejpeg-from-the-mean-of-all-elements-in-a-nested%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript